Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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
1  
you may have had a typo with "zips", so edit accordingly – Kevin Lochner Dec 10 '09 at 0:57
1  
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
2  
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

You must log in to answer this question.