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

I'm trying to modify a certain tar.gz archive I have. When I go to create the tar, it retains the parent directory structure, as a tarball should. However, is there a way to simply create a tar archive with just the files I specifiy, rather than the entire directory tree?

share|improve this question
Would you accept a way to remove directories on extraction instead of creation? As @yarek answered, finding and piping the files shows that tar doesn't support this. – Mike Fiedler May 7 '12 at 12:27
Well, some variants of tar do - see below. – yarek May 11 '12 at 20:13

Disabling recursion in tar.

POSIX pax(1) has -d option which enables cpio-like interface while allowing to create .tar files. Example from Wikipedia:

find . -depth -print | pax -wd > archive.tar

GNU tar(1) has an analogous option, --no-recursion. Example from there:

find dir tests | tar -cf archive -T - --no-recursion

P.S. I usually fall back to cpio(1) in such cases.

share|improve this answer

It's a little dirty, but yes:

michael@choad:~$ tar cf - -C ~/scratch packing_slip_411.pdf -C ~/bin/df_linux/ README.linux | tar tvf -
-rw-r--r-- michael/users 37686 2012-03-30 15:09 packing_slip_411.pdf
-rwx------ michael/users  1364 2012-03-30 07:27 README.linux
share|improve this answer

Specifying the full path of a file will preserve the tree:

tar cf newarchive.tar /path/to/file1 /path/to/file2

To omit the file structure, cd to the directory and create the archive:

(cd /path/to && tar cf /tmp/newarchive.tar file1 file2)
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.