我想将命令搜索到的文件复制find
到当前目录
# find linux books
find ~ -type f -iregex '.*linux.*\.pdf' -print0 | xargs -0 echo
# the result
../Books/LinuxCollection/Linux_TLCL-17.10.pdf ../Richard Blum, Christine Bresnahan - Linux Command Line and Shell Scripting Bible, 3rd Edition - 2015.pdf ..
Run Code Online (Sandbox Code Playgroud)
测试使用命令 `cp' 将文件复制到当前目录
find ~ -type f -iregex '.*linux.*\.pdf' -print0 | xargs -0 cp .
Run Code Online (Sandbox Code Playgroud)
获取错误:
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
Run Code Online (Sandbox Code Playgroud)
我解决了命令替换的问题
cp $(find ~ -type f -iregex '.*linux.*\.pdf' -print0) .
Run Code Online (Sandbox Code Playgroud)
如何完成它xargs
?
mur*_*uru 17
如cp
错误所示,目标目录必须放在最后。因为它看起来像你cp
没有GNU的等效cp
的-t
选项,你必须得到xargs的插入的文件名cp
和.
:
find ... | xargs -0 -I _ cp _ .
Run Code Online (Sandbox Code Playgroud)
where-I
用于告诉哪个字符串将被输入替换(在这种情况下,我使用的是_
,但{}
也常用)。
当然,这可以通过find
自己来完成:
find ~ -type f -iregex '.*linux.*\.pdf' -exec cp {} . \;
Run Code Online (Sandbox Code Playgroud)