簡體   English   中英

如何將具有相同后綴的所有文件復制到另一個目錄? -Unix

[英]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 )。

findxargs一起使用,如下所示:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM