Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

Fol1/Fol2
Fol3
Fol4/Fol5

And I want to move them all to a common base folder. How would I do this?

share|improve this question
    
Do you want to keep the directory structure? You want to move the directories, or just the zip files? –  jordanm Mar 10 '13 at 21:31
    
I just want to move the zip files. I don't really care about the directories - they can be left alone or deleted. –  InquilineKea Mar 10 '13 at 21:33

4 Answers 4

If you only want to move the .zip files, you could do something like

mkdir ./zipfiles
find . -name "*.zip" -exec mv "{}" ./zipfiles \;
share|improve this 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 {} \;
share|improve this answer

If you're using bash version 4 or higher or zsh you can also use recursive globbing:

mv **/*.zip /path/to/move/zip/files/to

This will move ONLY the files and not their relative paths, so collisions might occur.

share|improve this answer

As a bash hack, you could use the power of tar:

(cd origin/;find . -exec file --mime-type "{}" \;|grep "application/zip"|cut -d ':' -f 1|xargs tar cf -)|(cd target/; tar xf -) && rm -fr origin/

This has the advantage that it does not depend on file extensions, since it uses the file command to choose the files to move and it preserves the directory structure, but it doesn't work with paths containing spaces.

The final rm is there just to remove the original files (becouse @InquilineKea tolds that he doesn't care about the original folder in the comment).

Of course this is particularly suitable if the origin/ and target/ folders are on different disks. Otherwise the other solutions proposed are more efficient (even if they rely on naming conventions, instead of file's content).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.