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?
3 Answers
To copy only pdfs from all sub-directories, type:
rsync -rv --include '*/' --include '*.pdf' --exclude '*' /path/to/parent/source/directory/ /path/to/Destination/directory
-
-
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.– RaphaelNov 10, 2015 at 18:55
-
-
@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.– muruNov 11, 2015 at 6:49
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 {} +
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 {} +