I'd like to copy the files searched using find
command into the current directory. I am executing the following command-line:
# find linux books
$ find ~ -type f -iregex '.*linux.*\.pdf' -print0 | xargs -0 echo
# Expected output on STDOUT
../Books/LinuxCollection/Linux_TLCL-17.10.pdf ../Richard Blum, Christine Bresnahan - Linux Command Line and Shell Scripting Bible, 3rd Edition - 2015.pdf ..
I wish to copy the files to the current directory using the cp
command. Here is the command that I entered:
$ find ~ -type f -iregex '.*linux.*\.pdf' -print0 | xargs -0 cp .
However, I get an error as shown below upon executing the above command:
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
I have also tried to resolve the issue by using command substitution. Here is the command that I tried:
$ cp $(find ~ -type f -iregex '.*linux.*\.pdf' -print0) .
However, I get another error as shown below upon executing the above command:
cp: Blum,: No such file or directory
How do I accomplish the desired result using the xargs
command?
Best Answer
xargs
appends its input to the command, so you are basically runningcp . source_file
. Useinstead.