how do I tar.gz
everything apart from jpg/gif/png/wmv/flv/zips
, recursively, via the Unix shell?
3 Answers
Use the --exclude
option:
tar czvf myarchive.tar.gz --exclude=\*.{jpg,gif,png,wmv,flv,tar.gz,zip} rootdir
-
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
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.
find ./ -type f | egrep -v "\.(jpg|gif|png|wmv|flz|zips)$" | xargs tar -czf filename.tgz
-
1
-
1Do you want to be a $ at the end there? Won't your egrep filter out jpg.txt? Dec 10, 2009 at 1:09
-
2tar (c)reate + xargs can be dangerous if your arg length is long enough that tar is called twice. Dec 10, 2009 at 1:16