1

I know how to tar a bunch of files, minus several I don't want:

tar --exclude=./*.jpg -pcjvf all.tar.gz ./

Here I tar all files, except any jpg extensions.

I want to do the opposite. I need to only tar certain files. After googling I haven't had too much luck finding examples.

Does an --include option exist with the tar command?

4 Answers 4

3

There is no --include option, as you could simply attach all files you want to include as parameters to tar. If you want to include via wildcard, use find:

tar -pcjvf specific.tar.gz $(find -name *.jpg)
0

Am I missing something? Why not just do:

tar -pcjvf pictures.tar.gz ./*.jpg
0

Something like this:

tar -pcjvf images.tar.gz ./*.jpg ./*.jpeg ./*.png
0

I would do it by piping 'find' output to tar:

find /your/directory -type f -name '*.jpg' | tar --files-from=- -cvf meow.tar

The 'find' command prints all jpg's stored in /your/directory recursively, and tar's magical --files-from option will read the files from '-', signifying stdin, and tar them. I find this particularly useful because rsync also has a --files-from option, so I have to remember only one file-specifying command. Furthermore, 'find' is more powerful than tar or rsync's file specifying abilities.

You must log in to answer this question.