如何将所有具有相同后缀的文件复制到另一个目录?- Unix

alv*_*vas 2 unix linux copy file find

我有一个目录,其中包含未知数量的子目录和未知级别的子*目录。如何将所有具有相同后缀的文件复制到新目录中?

例如从这个目录:

> some-dir
  >> foo-subdir
    >>> bar-sudsubdir
      >>>> file-adx.txt
  >> foobar-subdir
    >>> file-kiv.txt
Run Code Online (Sandbox Code Playgroud)

将所有 *.txt 文件移至:

> new-dir
 >> file-adx.txt
 >> file-kiv.txt
Run Code Online (Sandbox Code Playgroud)

dev*_*ull 5

一种选择是使用find

find some-dir -type f -name "*.txt" -exec cp \{\} new-dir \;
Run Code Online (Sandbox Code Playgroud)

find some-dir -type f -name "*.txt"*.txt会在目录中找到文件some-dir。该选项为每个由 表示的匹配文件-exec构建一个命令行(例如) 。cp file new.txt{}

  • 我知道。旧习难改。 (2认同)