Question about using tar

Tim Chase blinux.list at
Wed Aug 26 16:10:32 UTC 2009

> I know how to extract files using tar with:
> tar zxvf filename.tar.gz
> However, how do I create an archive that includes the subdirectories?

The "x" is for "extract".  Change it to "c" to "create" the new 
compressed-gzipped file and then pass in the things you want to 
include in the archive:

   tar cvfz filename.tgz file1.txt dir2/ dir3/ file4.txt

As a kindness, usually you put all the files you want to unpack 
in a subdirectory and tar/gzip that one directory so that when 
you unzip it, you have a subdirectory with all the files inside, 
instead of littering the directory with the contents:

   tar cvfz out_file.tgz subdirectory/

By default, tar recurses into subdirectories, so any 
sub-sub-directories you have will be included.  If you don't want 
that behavior, GNU tar takes a "--no-recursion" parameter to 
override it.

The "v" (for "verbose") parameter is optional if you don't want 
to have it dump out all the files it's adding, so you can just 
use "cfz" to create and "xfz" to extract.  Order doesn't matter 
-- I default to putting the "z" ("compress") last because I think 
of it as "pass the tar options, and then tack on the compression".

As always, there's oodles more documentation at "man tar".

Hope this helps.


More information about the Blinux-list mailing list