Find all PDF files and move to a folder on macOS using xargs

I’m on macOS and I’m trying to search all the pdf’s in a directory and moved them to a folder.
Here’s what I’m attempting:

find . -iname '*.pdf' -print0 | xargs mv /PDFs

feels close but no cigar. How might I do this with a for loop as well…

Thanks

Welcome @piagetblix

You need to modify your syntax a little bit:

 find . -iname '*.pdf' -print0 | xargs -0 -I file mv "file" /PDFs

Where xargs options are:

  • -0 : Deal with special file names such as whitespace, quotes and other special characters in filenames.
  • -I file : Replace occurrences of “file” in the initial-arguments with names read from find command and pass it to the mv command. This is where magic happens and you were missing this arg.

I won’t recommend a bash for loop as it is too wild to deal with special file names.

1 Like

Thanks for the great explanation! So is the “file” like a parameter in that it could be anything between double quotes, not explicitly the word file?

Hmm, now I’m getting permission denied when I run the command…

Got it the last path of /PDFs of course needed to be ./PDF.

Yes, file is like a parameter for mv command. it can be anything, say “{}”. Yes, replcae /PDFs with actual path. If it is located at /PDFs

find . -iname '*.pdf' -print0 | xargs -0 -I {} mv "{}" /PDFs

HTH

Mind if I bother you with one more question on this? What are the {} doing just after the -I ?

cheers

It is same as file. My point was you can use file or {} or anything else as a parameter for mv.

1 Like