简体   繁体   English

Linux在子目录中找到所有文件并将其移动

[英]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.

相关问题 如何查找所有目录和子目录的文件总和(分别),并按大小对文件进行排序 - How to find sum of files for all directories and sub directories (separately) and them sort them by size 在linux中,我怎样才能把zip具体的子目录改成自己的zip文件名,把父目录名和Z78E6221F6393D1356DZF2Z文件全部改成a393D1356DZF21的单目录? - In linux, how can I zip specific sub directories into their own zip files named parent directory name and output them all into a single directory? 将多个文件移动到 linux 中的目录 - Move multiple files to directories in linux linux创建目录并将相应文件移动到目录 - linux create directories and move corresponding files to the directories 如何在Linux中的目录和子目录中的特定单词之下找到特定单词的文件/(文件的pwd) - how to find the files/(pwd of file) which is having a particular word below a particular word in directories and sub directories in linux 使用find - 删除除任何一个之外的所有文件/目录(在Linux中) - Using find - Deleting all files/directories (in Linux ) except any one 在Unix / Linux中查找没有文件的目录 - Find Directories With No Files in Unix/Linux 查找名称与pattern匹配的目录并移动它们 - Find directories with names matching pattern and move them linux脚本将带有数据的目录中的文件移动 - linux script to move files in directories with data 如何在 Linux 上找到当前目录的所有直接子目录? - How can I find all immediate sub-directories of the current directory on Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM