如何将目录树中的所有 HTML 文件复制到单个目录

sim*_*ico 7 osx find cp wildcards

我想将所有.html文件myDir及其子目录复制到~/otherDir. 这是我尝试过的,但不起作用:

$ find myDir -name *.html -print | xargs -0 cp ~/otherDir
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
Run Code Online (Sandbox Code Playgroud)

Mil*_*ach 12

首先,shell 正在为你通配 '*'。使用\或使用引号将其转义*.html

像这样:

find myDir -name "*.html" 或者 find myDir -name \*.html

跳过使用xargswithfind-exec开关:

find myDir -name "*.html" -exec cp {} ~/otherDir \;

这是有效的,因为{}它取代了find找到的文件,并且为每个匹配执行一次。

另请注意,这将使源目录的副本变平。例子:

myDir/a.html
myDir/b/c.html
Run Code Online (Sandbox Code Playgroud)

会屈服

otherdir/a.html
otherdir/c.html
Run Code Online (Sandbox Code Playgroud)


Mel*_*Mel 3

find myDir -name '*.html' -print0 | xargs -0 -J % cp % ~/otherdir