我有一个 Linux 系统,一些用户将文件与 ftp 放在一个目录中。在这个目录中有用户可以创建的子目录。现在我需要一个脚本来搜索这些子目录中的所有文件并将它们 move 到一个目录中(用于备份)。问题:不应删除子目录。
用户的目录是/files/media/documents/ 并且必须将文件 move 到目录/files/dump/中。我不关心/files/media/documents/中的文件,它们已经由另一个脚本处理。
我已经试过这个脚本了:
for dir in /files/media/documents/
do
find "$dir/" -iname '*' -print0 | xargs -0 mv -t /files/dump/
done
最佳答案
您可以只使用查找而不是迭代。在手册页中记录了一个“-type”选项,因此要 move 仅文件,您可以这样做:
find "/files/media/documents/" -type f -print0 | xargs -0 mv -t /files/dump/
您也不喜欢在/files/media/documents/中查找文件,但所有子目录?只需添加“-mindepth”:
find "/files/media/documents/" -type f -mindepth 1 -print0 | xargs -0 mv -t /files/dump/
或者,您也可以使用“-exec”来跳过第二个命令 (xargs):
find "/files/media/documents/" -type f -mindepth 1 -exec mv {} /files/dump/ \;
关于Linux 查找子目录中的所有文件并 move 它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655186/