Move all files with a certain extension from multiple subdirectories into one directory

command linefilesfindrecursive

I have a bunch of .zip files in several directories:

Fol1/Fol2
Fol3
Fol4/Fol5

How would I do move them all to a common base folder?

Best Answer

Go to the toplevel directory of the tree containing the zip files (cd …), then run

mv **/*.zip /path/to/single/target/directory/

This works out of the box in zsh. If your shell is bash, you'll need to run shopt -s globstar first (you can and should put this command in your ~/.bashrc). If your shell is ksh, you'll need to run set -o globstar first (put it in your ~/.kshrc).

Alternatively, use find, which works everywhere with no special preparation but is more complicated:

find . -name '*.zip' -exec mv {} /path/to/single/target/directory/ \;

If you want to remove empty directories afterwards, in zsh:

rmdir **/*(/^Fod)

In bash or ksh:

rmdir **/*/

and repeat as long as there are empty directories to remove. Alternatively, in any shell

find . -depth -type d -empty -exec rmdir {} \;
Related Question