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.

how do I tar.gz everything apart from jpg/gif/png/wmv/flv/zips, recursively, via the Unix shell?

share|improve this question

migrated from stackoverflow.com Dec 10 '09 at 0:47

This question came from our site for professional and enthusiast programmers.

3 Answers 3

Use the --exclude option:

tar czvf myarchive.tar.gz --exclude=\*.{jpg,gif,png,wmv,flv,tar.gz,zip} rootdir
share|improve this answer
Just make sure you're running gnu tar, otherwise this flag won't exist. On solaris you can specify an exclude file though. –  Darren Hall Dec 10 '09 at 1:07
find -type f -a ! \( \
                     -name '*.jpg' -o \
                     -name '*.gif' -o \
                     -name '*.png' -o \
                     -name '*.wmv' -o \
                     -name '*.flv' -o \
                     -name '*.tar.gz' -o \
                     -name '*.zip' \) \
 | xargs tar rvfz files.tgz

I used multilple lines to make it easier to edit later in case you made this into a shell script or shell function.

I use the -r flag for tar in case the number of files exceeds 1 call into xargs. This way you can't exceed the argument buffer for tar.

share|improve this answer

  find ./ -type f | egrep -v "\.(jpg|gif|png|wmv|flz|zips)$" | xargs tar -czf filename.tgz
share|improve this answer
you may have had a typo with "zips", so edit accordingly –  Kevin Lochner Dec 10 '09 at 0:57
Do you want to be a $ at the end there? Won't your egrep filter out jpg.txt? –  Suppressingfire Dec 10 '09 at 1:09
tar (c)reate + xargs can be dangerous if your arg length is long enough that tar is called twice. –  Darren Hall Dec 10 '09 at 1:16

Your Answer


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