我想将命令find搜索的文件复制到currernt目录
# 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 ..使用命令“`cp”测试将文件复制到当前dir
find ~ -type f -iregex '.*linux.*\.pdf' -print0 | xargs -0 cp .获取错误:
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我用命令替换解决了问题
cp $(find ~ -type f -iregex '.*linux.*\.pdf' -print0) .如何用xargs来实现呢?
发布于 2018-03-28 02:09:56
正如cp错误所指出的,目标目录必须是最后一个。由于您的cp似乎没有与GNU cp's -t选项相当的名称,所以必须让xargs在cp和.之间插入文件名:
find ... | xargs -0 -I _ cp _ .其中,-I用于判断哪个字符串将被输入替换(在本例中,我使用的是_,尽管{}也是常用的)。
当然,这可以通过find本身来完成:
find ~ -type f -iregex '.*linux.*\.pdf' -exec cp {} . \;https://unix.stackexchange.com/questions/433938
复制相似问题