2015-06-19 69 views
0

我知道的非常奇怪而具体的问题,但会为我节省一大笔头痛。Centos:合并目录,同时删除所有子目录中的公用子目录

是否可以合并目录,同时删除每个子文件中的特定子文件但保留所有文件?例如

/first/1/files/1.jpg, 2.jpg, 3.jpg 
/first/2/files/1.jpg, 2.jpg, 3.jpg 

我也有

/second/1/3.jpg, 4.jpg, 5.jpg 
/second/2/3.jpg, 4.jpg, 5.jpg 

我想合并成/first同时/second/first也消除了其间的/files/目录但保留所有的JPG文件。最重要的是,我不想3.JPG在/second/

+0

任何你想保留副本的常见文件? –

+0

是的,如果可能的话,如果他们覆盖那不是什么大问题,因为他们应该是同一个文件我只是不是100% –

回答

1

被覆盖。如果时间戳没有事,那么你可以这样做:

find /second -name '*.jpg' -exec touch + 
for fdir in /first/*/files; do 
    sdir=${fdir%/files}; 
    sdir=${sdir#/tmp/first}; 
    sdir=/tmp/second${sdir}; 
    cp -u "$fdir/"* "$sdir/"; 
done 

尽管这不正是我想称好的解决方案。

你也可以做到这一点手动测试现有的文件,同时循环和复制这样的:

for ffile in /first/*/files/*.jpg; do 
    # Expand the variable and delete '/files' 
    sfile=${ffile/\/files} 
    # Expand the variable and remove the '/first' prefix 
    sfile=${sfile#/tmp/first} 
    # Prepend the new '/second' direcory. 
    sfile=/second${sfile} 
    # If the exist do nothing. It if doesn't then copy. 
    [ -f "$sfile" ] || cp -u "$ffile" "$sfile" 
done 

我觉得有可能是一个聪明的方式使用rsync或cpio这样做,但我不知道那是什么。

+0

谢谢你的回复,我有点紧张,试试这个,因为我有不知道我在做什么循环的命令行。 –

+0

在每个脚本'echo cp ....'的'cp'之前粘上'echo',它可以安全地作为测试运行。它会输出它本来会运行的命令。这是唯一真正做到的事情。 –

+0

或者,如果数据允许它在运行之前进行复制/备份。您还可以在脚本的顶部添加'set -x'来让shell列出运行时运行的每个命令。 –