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?



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.

  • 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*

  • 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
  • @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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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