How to move output of find command to another directory?

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?

1

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 /destination with the actual destination directory
  • find will handle all possible filenames
  • find will handle ARG_MAX by passing as many filenames in one go so that does not trigger ARG_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 as find will 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') ./destination

The $() applies the output from the command run (find /home -type f -name '*.pdf') to the command outside of it (mv [...] ./destination).

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like