Server Fault is a question and answer site for system and network administrators. 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

What is the best way to archive select file extensions in an unknown tree. I cannot use exclude options in tar/gzip/rsync as it would be impossible to exclude all possible extension variations.

Example to backup all .foo and .bar files in a recursive tree but not other unknown file types.

I presume this will need to be a combination of ls/find, grep and tar/gzip.

Thank you

share|improve this question
up vote 2 down vote accepted

Very similar to dmourati's answer, but without xargs.

find /path/to/tree \( -name "*.foo" -o -name "*.bar" \) -print0 | tar -T /dev/stdin --null -cvzf foobar.tar.gz
share|improve this answer
Thank you - Accepted solution – Linux Tar Archive query May 21 '11 at 3:29

Try something like this:

find /path/to/tree \( -name "*.foo" -o -name "*.bar" \) -print0 | xargs -r0 tar cvzf foobar.tar.gz

You generate the list of files via the find command and then pipe that to xargs and on to tar.

share|improve this answer
This could cause issues if there are too many files for xargs to put in a single command. – Ignacio Vazquez-Abrams May 21 '11 at 3:16
There are a lot of them and the tree does get quite deep in places – Linux Tar Archive query May 21 '11 at 3:18
Maybe just me but it seams to be ignoring the first file extension and just tar'ing the second. – Linux Tar Archive query May 21 '11 at 3:23
Editted to fix the grouping – dmourati May 21 '11 at 3:23
Thats fixed it - Thank you. – Linux Tar Archive query May 21 '11 at 3:27

Your Answer


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.