Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a simple question, yet I can't find or solve the answer. I want to make a tar archive, but I want to exclude some files from it using regular expression.

Example of the file to exclude is this: 68x640X480.jpg

I have tried this with no luck:

tar cvf test.tar --exclude=[0-9]+x[0-9X]+\.jpg /data/foto

Can anybody help ?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You can use some additional tools like find and egrep:

find directory/ -type f -print | egrep -v '[0-9]+x[0-9X]+\.jpg' | tar cvfz directory.tar.gz -T -

The drawback of the above mentioned method is that it will not work for all possible file names. Another opportunity is to use the built-in exclude functionality of tar:

tar -czvf directory.tar.gz --exclude='*x*X*.jpg' directory

Unfortunately the second method does not work with regular expressions, but only with wildcards.

share|improve this answer
    
Thanks, this is what I was looking for. Can you please make a note about what file names wouldn't work ? e.g. containing what characters ? –  Frodik Oct 12 '11 at 9:46
    
You shouldn't worry too much about that - most file names work perfectly fine with that solution. The problem is that UNIX/Linux file names can include pretty much everything, even control characters - such obscure combinations will not work with the first solution. –  Vladimir Blaskov Oct 12 '11 at 10:15
    
A nice read related to UNIX/Linux/POSIX file names: dwheeler.com/essays/fixing-unix-linux-filenames.html –  Vladimir Blaskov Oct 12 '11 at 10:16

Maybe you should try cpio

http://cybertiggyr.com/cpio-howto

It reads from file list and does the archiving. You can generate a file list using sed like given below.

ls |sed   '/[0-9]*x[0-9]*X.*/d' >/tmp/files

You can then use it as the input to cpio.

share|improve this answer

Your Answer

 
discard

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

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