簡體   English   中英

Bash 腳本不移動文件

[英]Bash script not moving files

我有一個 bash 腳本,旨在在 Linux 目錄中運行,該目錄僅包含一組不同格式的圖像文件和視頻文件。 執行后,腳本會查看 Vids 和 Pics 子目錄是否存在,如果不存在,則創建它們。 然后所有的圖像文件都應該移到 Pics 中,視頻文件移到 Vids 中。

但是當腳本執行時,目錄被創建但沒有文件被移動到其中。

那里的任何 bash 專家都可以快速查看並提出修復建議?

#!/bin/bash

echo "This script will check for the existence of 'Vids' and 'Pics' subdirectories and create them if they do not exist. It will then move all image files into 'Pics' and all video files into 'Vids'. Do you wish to proceed? (y/n)"
read proceed

if [ $proceed == "y" ]; then
  if [ ! -d "Vids" ]; then
    mkdir Vids
  fi
  if [ ! -d "Pics" ]; then
    mkdir Pics
  fi
  find . -name "*.jpg" -o -name "*.jpeg" -o -name "*.png" -o -name "*.gif" -exec mv {} Pics/ \;
  find . -name "*.mp4" -o -name "*.avi" -o -name "*.mkv" -o -name "*.wmv" -exec mv {} Vids/ \;
  echo "Image files have been moved to 'Pics' and video files have been moved to 'Vids'."
else
  echo "Exiting script."
fi

我將腳本命名為 test.sh 並授予它執行權限。 當我運行腳本時,它在一個包含大量圖像和視頻文件的目錄中運行。 劇本問我是否要繼續。 當我說是時,它說 Vids 和 Pics 目錄已創建,所有文件都已移入其中。 然后腳本結束。 但是,盡管創建了 Vids 和 Pics 目錄,但沒有任何文件被移動。

隱式AND運算符的優先級高於-o ,因此您的命令等同於:

find . -name "*.jpg" -o -name "*.jpeg" -o -name "*.png" -o \( -name "*.gif" -exec mv {} Pics/ \; \)

所以它只對*.gif執行-exec ,而不執行其他擴展。 您需要將所有-name表達式括起來。

find . \( -name "*.jpg" -o -name "*.jpeg" -o -name "*.png" -o -name "*.gif" \) -exec mv {} Pics/ \;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM