[英]How to copy all files from a directory and its subdirectories to another dir?
[英]How to copy all the files with the same suffix to another directory? - Unix
我有一個目錄,其中子目錄數未知,其中子目錄的級別未知。 如何將所有帶有相同后綴的文件復制到新目錄?
例如從此目錄:
> 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"
將在some-dir
目錄中找到*.txt
文件。 -exec
選項為每個由{}
表示的匹配文件建立命令行(例如cp file new.txt
)。
將find
與xargs
一起使用,如下所示:
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
。 因此,使用xargs
版本的fork / exec調用將更少。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.