I want the output of this command find /home -name *.pdf
to move the files in the search result to another directory, e.g. with a pipe. How can I do that?
2 Answers
You don't need any pipe-ing, and xargs to make the filenames as arguments for mv.
Just use mv within the -exec action of find:
find /home -type f -name '*.pdf' -exec mv -t /destination {} + - Replace
/destinationwith the actual destination directory findwill handle all possible filenamesfindwill handleARG_MAXby passing as many filenames in one go so that does not triggerARG_MAX- If you are looking for only files (presumably in this case), limit the search vector by adding
-type f - Quote the glob expansion,
'*.pdf', so that shell does not expand them beforehand asfindwill handle them
If for some weird reason, or for learning purpose, you must use pipe-xargs:
find /home -type f -name '*.pdf' -print0 | xargs -0 mv -t /destination 0 If you don't have spaces in the names of your pdf's, another way to do it is to run this command in your terminal:
mv $(find /home -type f -name '*.pdf') ./destinationThe $() applies the output from the command run (find /home -type f -name '*.pdf') to the command outside of it (mv [...] ./destination).