Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a lot of music in a lot of directories:

./artist1/album1/*.mp3
./artist1/album2/*.mp3
./artist1/album3/*.mp3
./artist2/album1/*.mp3
./artist2/album2/*.mp3
./artist3/album1/*.mp3
./artist3/album2/*.mp3
... ...

How can I zip them to this:

./artist1/album1/*.mp3  => ./artist1-album1.tar.gz
./artist1/album2/*.mp3  => ./artist1-album2.tar.gz
./artist1/album3/*.mp3  => ./artist1-album3.tar.gz
./artist2/album1/*.mp3  => ./artist2-album1.tar.gz
./artist2/album2/*.mp3  => ./artist2-album2.tar.gz
./artist3/album1/*.mp3  => ./artist3-album1.tar.gz
./artist3/album2/*.mp3  => ./artist3-album2.tar.gz
... ...

I'd like one-line-command or a simple script.

share|improve this question
1  
do you want to ZIP or do you want to TAR and GZIP? –  akira Dec 30 '10 at 10:14

1 Answer 1

up vote 2 down vote accepted

tar:

for dir in */*/; do dir=${dir%/}; tar cf "${dir//\//-}.tar" "$dir"; done

zip:

for dir in */*/; do dir=${dir%/}; zip -0r "${dir//\//-}.zip" "$dir"; done

MP3 files contain already-compressed data, which cannot be compressed further - the best you would get is ~2%, and it would be many times slower - so the first example is just a tar archive.

If you still want Gzip compression, add a z option (as in tar czf) and replace .tar with .tar.gz. Similarly, the -0 option to zip disables compression - change -0r to -r to re-enable.


${dir%/} removes the ending / from $dir

${dir//\//-} replaces all / with -

share|improve this answer
    
+1 for also mentioning that mp3s are already squashed up. –  Kez Dec 30 '10 at 13:27
    
+1 very helpful –  kev May 16 '12 at 5:49

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.