我有一个目录,其中包含未知数量的子目录和未知级别的子*目录。如何将具有相同后缀的所有文件复制到新目录?
例如从这个目录:
> some-dir
>> foo-subdir
>>> bar-sudsubdir
>>>> file-adx.txt
>> foobar-subdir
>>> file-kiv.txt
将所有 *.txt 文件移动到:
> new-dir
>> file-adx.txt
>> file-kiv.txt
一种选择是使用find:
find some-dir -type f -name "*.txt" -exec cp \{\} new-dir \;
find some-dir -type f -name "*.txt"会在目录中找到*.txt文件some-dir。该选项为每个由 . 表示的匹配文件-exec构建一个命令行(例如) 。cp file new.txt{}
使用findwithxargs如下所示:
find some-dir -type f -name "*.txt" -print0 | xargs -0 cp --target-directory=new-dir
对于大量文件,此xargs版本比使用更有效,find some-dir -type f -name "*.txt" -exec cp {} new-dir \;因为xargs一次将多个文件传递给,而不是每个文件cp调用一次。因此,该版本cp的 fork/exec 调用将更少。xargs