我有很多包含pdf
文件和其他扩展名的不同文件夹。我想将所有pdf
文件复制到另一个目的地。但是如果我用这样的查找来做到这一点:
"${SEARCHDIR}" -iname "*.pdf" | xargs -d "\n" -I {} cp -r --backup=t {} "${OUTPUTFOLDER}/"
我将所有pdf
文件放在一个目的地,这对我来说太乱了。出于这个原因,我想复制包括父目录在内的匹配文件,但不复制该目录中的不匹配文件。
输入:
user/search/test/sub
user/search/test/sub2
user/search/test/file.pdf
user/search/test/file.doc
user/search/test2/sub
user/search/test2/sub2/file2.pdf
user/search/test2/file3.pdf
user/search/test2/file.doc
应复制为
/destination/test/file.pdf
/destination/sub2/file2.pdf
/destination/test2/file3.pdf
编辑: 我不想复制整个路径。我不希望我的输出是这样的:
/destination/test/file.pdf
/destination/test2/sub2/file2.pdf
/destination/test2/file3.pdf
这个怎么做?
在 中
zsh
,您可以执行以下操作:从哪里
${file:t2}
获取文件的两部分尾部,即文件名及其父目录的名称。然后我们将该h
路径的开头创建为一个目录,并将文件复制到那里。