tar

tar


Overview



tar puts multiple files into a single (tar) file.
gzip compresses one file (only).

So, to get a compressed archive, you combine the two, first use tar to get all files into a single file (=archive.tar), then gzip it (=archive.tar.gz).

If you have only one file, you need to compress (notes.txt): there's no need for tar, so you just do gzip notes.txt which will result in notes.txt.gz. There are other types of compression, such as compress, bzip2 and xz which work in the same manner as gzip (apart from using different types of compression of course).


To preserve file permissions, use tar:
tar cpvf backup.tar folder



The p flag will save file permissions. Use the z flag for gzip compression or the j flag for bzip compression.
tar czpvf backup.tar.gz folder #backup.tgz is acceptable as well
tar cjpvf backup.tar.bz2 folder #backup.tbz2 works too



If you want to have a tar file you can "update" package the tar using the P flag:
tar cpPvf backup.tar folder


Then to update, replace 'c' with 'u' and when unpacking, you can use 'k' to preserve files that already exist.
tar upPvf backup.tar folder #updating a tar file
tar xpPkvf backup.tar #extracting a tar with permissions(p) and not extracting(k) files that exist on disk already
tar cjpvf backup.tar.bz2 folder #backup.tbz2 works too


The P flag saves files with full paths, so - /home/username vs home/username (notice the leading forward slash).

7z compression offers greater compression, but does not preserve file ownership, permissions, etc. as well.



Examples


tar -cf archive.tar file1 file2

Create archive archive.tar containing files file1 and file2. Here, the c tells tar you will be creating an archive; the f tells tar that the next option (here it's archive.tar) will be the name of the archive it creates. file1 and file2, the final arguments, are the files to be archived.

tar -tvf archive.tar

List the files in the archive archive.tar verbosely. Here, the t tells tar to list the contents of an archive; v tells tar to operate verbosely; and f indicates that the next argument will be the name of the archive file to operate on.

tar -xf archive.tar

Extract the files from archive archive.tar. x tells tar to extract files from an archive; f tells tar that the next argument will be the name of the archive to operate on.

tar -xzvf archive.tar.gz

Extract the files from gzipped archive archive.tar.gz verbosely. Here, the z tells tar that the archive will be compressed with gzip.

tar -cf archive.tar mydir/

Creates an archive of the directory mydir.

tar -czf archive.tar.gz mydir/

Creates an gzip-compressed archive of the directory mydir.

tar -zxvf myfile.tar.gz

Extract the contents of the myfile.tar.gz into the current directory.

tar -xvf archive.tar documents/work/budget.doc

Extract only the file documents/work/budget.doc from the archive archive.tar. Produce verbose output.

tar -xvf archive.tar documents/work/

Extract only the directory documents/work/, and any files it contains, from the archive archive.tar. Produce verbose output.

tar -xvf archive.tar --wildcards '*.doc'

Extract only files with the extension .doc from the archive archive.tar. The --wildcards option tells tar to interpret wildcards in the name of the files to be extracted; the file name (*.doc) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell.

tar -rvf archive.tar documents/work/budget.doc

Add the file documents/work/budget.doc to the existing archive archive.tar. The r option is the same as the long option --append.

tar -uvf archive.tar documents/work/budget.doc

Add the file documents/work/budget.doc to the archive archive.tar only if it is newer than the version already in the archive (or does not yet exist in the archive). Here, u is the same as the long option --update.

tar -cf - documents/work/ | wc -c

Estimate the file size of an archive of the directory documents/work, but do not create the file. Here, the archive file is specified as a dash ("-"), which tells tar to send its archived output to the standard output rather than a file on disk. This output is then piped to the wc command, which reports how many bytes (-c) were in the input it received.

tar -czf DogPhotos.tar.gz --exclude='kitty.jpg' MyPetPhotos

Create DogPhotos.tar.gz of all files contained in the MyPetPhotos without the kitty.jpg photo.

tar tf hope.tar.gz | grep myfile.txt

Search the hope.tar.gz file for the file myfile.txt and list the full path of the file. The returned results would resemble the line shown below.
/homedir/public_html/data/myfile.txt

tar -zxvf hope.tar.gz /homedir/public_html/data/myfile.txt

In the above example, the tar command would extract the one file myfile.txt from the hope.tar.gz. The full path to this file was determined by using the example shown earlier.
Full Flag Listings