Linux 找到子目录中的所有文件并移动它们
Linux find all files in sub directories and move them
我有一个 Linux 系统,一些用户将带有 ftp 的文件放在目录中。在这个目录中有用户可以创建的子目录。现在我需要一个脚本来搜索这些子目录中的所有文件并将它们移动到一个目录中(用于备份)。问题:不应删除子目录。
用户目录是/files/media/documents/
并且必须将文件移动到目录 /files/dump/ 中。我不关心 /files/media/documents/ 中的文件,它们已经由另一个脚本处理了。
我已经尝试过这个脚本:
for dir in /files/media/documents/
do
find "$dir/" -iname '*' -print0 | xargs -0 mv -t /files/dump/
done
您可以只使用查找而不是迭代。在手册页中记录了一个“-type”选项,因此要移动仅文件,您可以这样做:
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 系统,一些用户将带有 ftp 的文件放在目录中。在这个目录中有用户可以创建的子目录。现在我需要一个脚本来搜索这些子目录中的所有文件并将它们移动到一个目录中(用于备份)。问题:不应删除子目录。
用户目录是/files/media/documents/ 并且必须将文件移动到目录 /files/dump/ 中。我不关心 /files/media/documents/ 中的文件,它们已经由另一个脚本处理了。
我已经尝试过这个脚本:
for dir in /files/media/documents/
do
find "$dir/" -iname '*' -print0 | xargs -0 mv -t /files/dump/
done
您可以只使用查找而不是迭代。在手册页中记录了一个“-type”选项,因此要移动仅文件,您可以这样做:
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/ \;