Linux 将最新修改的文件从所有子目录复制到具有相同子目录结构的新目录

Linux 将最新修改的文件从所有子目录复制到具有相同子目录结构的新目录,linux,bash,rsync,cp,Linux,Bash,Rsync,Cp,我有一个目录,有100个子目录,每个目录有100个文件。每个子目录中的文件数量每天增加一个。我想在Linux中编写一个脚本,在另一个位置复制所有100个子目录,每个子目录只有一个最新文件。如何做到这一点?从当前目录(有100个子目录)可以按以下方式运行脚本: for f in `find ./* -maxdepth 0 -type d` do if [ $f != "./directory_to_copy" ] then

我有一个目录,有100个子目录,每个目录有100个文件。每个子目录中的文件数量每天增加一个。我想在Linux中编写一个脚本,在另一个位置复制所有100个子目录,每个子目录只有一个最新文件。如何做到这一点?

从当前目录(有100个子目录)可以按以下方式运行脚本:

   for f in `find ./* -maxdepth 0 -type d`
do
        if [ $f != "./directory_to_copy" ]
        then
                  cp $f/`ls -t $f | head -1` ./directory_to_copy
        fi
 done
如果您的out目录不在当前目录中,您可以跳过if语句,并且您可以指定需要在cp语句中以名称存储的目录的相对路径或绝对路径,如下所示:

cp $f/`ls -t $f | head -1` path_to_directory/name_of_directory

可以将if块修改为:

if [ $f != "./directory_to_cop" ]
    then
              mkdir ./directory_to_copy/$f 2>/dev/null
              cp $f/`ls -t $f | head -1` ./directory_to_copy/$f
    fi
如果out目录不在当前目录中:

mkdir path_to_directory/name_of_directory/$f 2>/dev/null

cp $f/`ls -t | head -1` path_to_directory/name_of_directory/$f

谢谢。这将复制每个子目录中的所有最新文件,但将它们放在单个输出目录中。有没有办法在out directory中保持相同的目录结构,以包含所有100个子目录,每个子目录都包含单个最新文件。您可以将if块修改为:
if[$f!=”/directory\u to\u copy“]然后mkdir./directory_to_copy/$F2/dev/null cp$f/
ls-t$f | head-1`./directory_to_copy/$f fi`如果out目录不在当前目录中:
mkdir path_to_directory/name_of_directory/$f 2/dev/null cp$f/
ls-t$f | hecad-1`path_to_to_directory/name_of_/$f`请出示你的问题。您应该至少包括您遇到问题的代码的大纲(但最好是a),然后我们可以尝试帮助解决特定问题。你也应该阅读。