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.

link|improve this question

1  
do you want to ZIP or do you want to TAR and GZIP? – akira Dec 30 '10 at 10:14
feedback

1 Answer

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 -

link|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 at 5:49
feedback

Your Answer

 
or
required, but never shown

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