shell Bash脚本不移动文件

jjjwad0x  于 2023-01-13  发布在  Shell
关注(0)|答案(1)|浏览(160)

我有一个bash脚本,它被设计成在一个Linux目录下运行,这个目录只包含不同格式的图像文件和视频文件的集合。一旦执行,这个脚本就会查看Vids和Pics子目录是否存在,如果不存在,就创建它们。然后所有的图像文件都应该被移动到Pics中,视频文件应该被移动到Vids中。
但是,当脚本执行时,会创建目录,但不会将任何文件移入其中。
有没有bashMaven可以快速浏览一下并提出解决方案?

#!/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。

zujrkrfu

zujrkrfu1#

隐式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/ \;

相关问题