6

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?

Thanks!

hsatterwhite
  • 332
  • 2
  • 5
  • 14

4 Answers4

9

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*

B14D3
  • 5,188
  • 15
  • 64
  • 83
  • 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
7

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.

MadHatter
  • 79,770
  • 20
  • 184
  • 232
1

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

al0
  • 11
  • 1
0

For multiple files with different patterns, one could use

find ./ -type f \( -name "apple*" -o -name "orange*" -o -name "berries*" -o -name "lemons*" \) -exec tar -cf archive.tar '{}' \+

This is based on the answers from B14D3 and al0, and the link here : https://www.tecmint.com/linux-find-command-to-search-multiple-filenames-extensions/

Dilhan
  • 1