[英]Linux find all files in sub directories and move them
I have a Linux-System where some users put files with ftp in a Directory. 我有一个Linux系统,其中一些用户将带有ftp的文件放在目录中。 In this Directory there are sub-directories which the users can create. 在此目录中,有用户可以创建的子目录。 Now I need a script that searches for all files in those subdirectories and moves them in a single Directory (for backup). 现在,我需要一个脚本来搜索那些子目录中的所有文件,并将它们移动到单个目录中(以进行备份)。 The Problem: The Sub directories shouldn´t be removed. 问题:子目录不应该被删除。
the directory for the users is /files/media/documents/ and the files have to be moved in the Directory /files/dump/. 用户的目录是/ files / media / documents /,并且文件必须在目录/ files / dump /中移动。 I don´t care about files in /files/media/documents/, they are already handled by another script. 我不在乎/ files / media / documents /中的文件,它们已经由另一个脚本处理。
I already tried this script: 我已经尝试过此脚本:
for dir in /files/media/documents/
do
find "$dir/" -iname '*' -print0 | xargs -0 mv -t /files/dump/
done
Instead of iterating, you could just use find. 除了迭代外,您还可以使用find。 In man-page there is a "-type" option documented, so for moving only files you could do: 在手册页中记录了一个“ -type”选项,因此对于仅移动文件,您可以执行以下操作:
find "/files/media/documents/" -type f -print0 | xargs -0 mv -t /files/dump/
You also won't like to find files in /files/media/documents/, but all sub-directories? 您也不想在/ files / media / documents /中找到文件,但是所有子目录都可以? Simply add "-mindepth": 只需添加“ -mindepth”:
find "/files/media/documents/" -type f -mindepth 1 -print0 | xargs -0 mv -t /files/dump/
Alternatively you could also use "-exec" to skip a second command (xargs): 或者,您也可以使用“ -exec”跳过第二条命令(xargs):
find "/files/media/documents/" -type f -mindepth 1 -exec mv {} /files/dump/ \;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.