4

I have a folder with about 20 subfolders, each with 10-15 subfolders. Is there a way, using CLI to move only PDf files to a new folder?

1
  • use rsync with arguments.
    – Raphael
    Nov 10, 2015 at 18:33

3 Answers 3

4

To copy only pdfs from all sub-directories, type:

rsync -rv --include '*/' --include '*.pdf' --exclude '*' /path/to/parent/source/directory/ /path/to/Destination/directory
5
  • Glad! it helped.
    – Raphael
    Nov 10, 2015 at 18:53
  • out of curiosity, what is the difference between the command you posted and this one: find . -name "*.pdf" -type f -exec cp {} ./destination_folder \; Nov 10, 2015 at 18:53
  • rsync is a different program that copies, find is program that find files and exec is a program that executes arguments. So, when you use find and exec together it finds the files and executes the arguments given by the user.
    – Raphael
    Nov 10, 2015 at 18:55
  • @simplemind - : )
    – Raphael
    Nov 10, 2015 at 18:58
  • @Raphael exec is a shell built-in, true, but the -exec seen with find is an option of find itself, and not an external command.
    – muru
    Nov 11, 2015 at 6:49
4

You can use cp with bash's globstar enabled (see this U&L question on recursive globs):

shopt -s globstar 
cp some/folder/**/*.pdf target/folder

Or, find:

find some/folder -iname '*.pdf' -exec cp -t target/folder {} +
0
2

find with -exec flag is appropriate for that. It by default is recursive and descends into subfolders

If I wanted to move all png files from Downloads to Downloads/PNG folder, I would do this: find $HOME/Downloads -type f -iname "*.png" -exec mv -t $HOME/Downloads/PNG {} +

In your case,

find /path/to/top/folder -type f -iname "*.pdf" -exec mv -t /some/other/place {} +

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .