4

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

0

3 Answers 3

6

Use the --exclude option:

tar czvf myarchive.tar.gz --exclude=\*.{jpg,gif,png,wmv,flv,tar.gz,zip} rootdir
1
  • Just make sure you're running gnu tar, otherwise this flag won't exist. On solaris you can specify an exclude file though. Dec 10, 2009 at 1:07
2
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.

2

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

You must log in to answer this question.