将多个文件夹中的 Mp4 文件提取到一个文件夹中

将多个文件夹中的 Mp4 文件提取到一个文件夹中

我的 /Downloads 文件夹中目前有数百个包含 mp4 文件的文件夹

我怎样才能一次性将所有 mp4 从子文件夹移动到 1 个文件夹中?此外,子文件夹名称各不相同,唯一的共同点是它们都包含 mp4 文件。

答案1

假设每个 .mp4 文件都有一个唯一的文件名,您可以执行以下操作:

find ~/Downloads -type f -iname "*.mp4" -exec cp -av "{}" /path/to/destination/ \;

这将找到您的下载文件夹中的所有 .mp4 文件并将其复制到一个文件夹中。

如果你确实想要,你可以用 替换cp -avmv -v这会将文件移动到新目标,而不是复制它们。

答案2

一个简单的 bash 脚本就可以为您完成此操作。

#!/bin/bash
# A script to move files to a directory from all found under the working directory.
for i in $(find "$PWD" -type f | grep .mp4 ); do
    # Name of file to use in output file for further prosessing
    NAME=$(basename "$i") 
    # Directory of the file to be moved.
    DIR=$(dirname "$i")
    # move the file to the directory passed in on the command line located in the current working directory.
    mv "$DIR/$NAME" "$PWD/$1"      
done

从我的视频处理脚本来看,我从不允许文件中出现任何非法字符或空格。如果有的话,希望“”能正常工作。在开头为该行添加一个 echo “$DIR/$NAME”,以确认它会按预期工作并提供文件的完整路径。我正在用该脚本在临时目录中试验我的音频文件。最好在您自己的重复文件上试一试。该脚本将要移动到的目录置于工作目录中。这将是您的所有文件所在的顶级目录,它将搜索此目录下的所有 .mp4 文件,并将它们移动到您希望所有单个文件所在的顶级目录中。我在测试的这个顶级目录中有我的脚本副本。

MacUser2525:/Volumes/Sea_To_Do/working/38_Special$ ./test_move.sh
snip ....
/Volumes/Sea_To_Do/working/38_Special/38_Special_1991_Bone_Against_Steel/38_Special-Bone_Against_Steel-13-Treasure.m4a

它显示了正确的名称和路径,因此继续测试实际移动。现在要移动的文件数量。

MacUser2525:/Volumes/Sea_To_Do/working/38_Special$ find . -type file | grep .m4a  | wc -l
      69

目标目录。

MacUser2525:/Volumes/Sea_To_Do/working/38_Special$ find test -type file | grep .m4a  | wc -l
       0

使用 -v 命令运行的命令用于移动以显示其所执行的操作。

MacUser2525:/Volumes/Sea_To_Do/working/38_Special$ test_move.sh test
snip ....
/Volumes/Sea_To_Do/working/38_Special/38_Special_1991_Bone_Against_Steel/38_Special-Bone_Against_Steel-13-Treasure.m4a -> /Volumes/Sea_To_Do/working/38_Special/test/38_Special-Bone_Against_Steel-13-Treasure.m4a

移动后的目标目录。

MacUser2525:/Volumes/Sea_To_Do/working/38_Special$ find test -type file | grep .m4a  | wc -l
      69

成功 我将所有 69 个文件从不同的目录移动到单个目录目标。它应该为你做同样的事情,没有任何古怪的命名,保证就像它刚刚为我做的一样。文件名中的空格和类似垃圾没有任何线索。我认为“”可以做到这一点,但我从未处理过这样的文件。

相关内容