Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

I want to create a tar ball and include only the files in the directory that start with a specific filename. For instance I have apples-x.x.x and oranges-x.x.x, and I only want to tar the files beginning with "apple". How can I do this?


share|improve this question
up vote 5 down vote accepted

Ummm, I may be overlooking something, but how about

tar cf /path/tarfile appl*

Unless you have directories which are also called ./appl*, in which case they'll get trawled up, with their contents, that should do it. If the latter pertains, you can stop that with --no-recursion.

share|improve this answer
Thanks this is excellent and spot on for what I need to do. – hsatterwhite Feb 21 '11 at 17:17

Maybe something like that:

find / -type f -name "apple*" -exec tar -rf archive.tar '{}' \;

Its better than tar cf /path/tarfile appl* beacause dont archive directory with pattern apple*

share|improve this answer
If I'm not mistaken, this will run the tar command multiple times, each time creating a tarfile called archive.tar with one apple* file in it; the upshot will be that after the find finishes, you'll have a tarfile containing only the last apple* file to be found by find. I'm guessing this isn't what you intend? – MadHatter Feb 21 '11 at 16:27
yes my mistake... it should be tar -rf – B14D3 Feb 21 '11 at 16:31
now it works (i think) in the way you intend – B14D3 Feb 21 '11 at 16:35
This is great, thanks for posting this answer! – hsatterwhite Feb 21 '11 at 17:18
@hsatterwhite thx :D – B14D3 Feb 17 '12 at 11:16

What about

find / -type f -name "apple*" -exec tar -cf archive.tar '{}' \+

? This way it would pass all file names to tar in a single shot (see -exec command {} + on the find man page).

share|improve this answer

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.