Name
tar [man page] - The GNU version of the tar archiving utility
Synopsis
tar [ - ] A --catenate --concatenate | c --create
| d --diff --compare | --delete | r --append
| t --list | u --update | x --extract --get [ options ]
pathname [ pathname ... ]
Frequently used options
-f, --file [HOSTNAME:]F
use archive file or device F (default "-",
meaning stdin/stdout)
-j, --bzip2 filter archive through bzip2,
use to decompress .bz2 files.
-N, --after-date DATE, --newer DATE
only store files newer than DATE
-v, --verbose
verbosely list files processed
-w, --interactive, --confirmation
ask for confirmation for every action
-z, --gzip, --gunzip, --ungzip
filter the archive through gzip
Examples
For the sake of simplicity we show linux tar examples without using a tape drive. Many of the tar examples can be translated to use tape drive. For example: Create a archive of /etc/ directory to a file etc.tar
tar -cf etc.tar /etc
to create a archive to the tape (/dev/st0 ) instead of archive file we can translate this command to:
tar -cf /dev/st0 /etc
During this tar tutorial we will use "archivewithtar/" directory as sample directory. Here is a content of this sample directory:
Create an archive to linuxtarfile.tar file of the archivewithtar/ directory:
tar cf linuxtarfile.tar archivewithtar/
To see what files are being archived by tar command we can use - v ( tar verbose mode ):
tar cf linuxtarfile.tar archivewithtar/
To instruct tar command to list all files within a archive file we can use command:
tar tf linuxtarfile.tar
To extract just particular files from a tar archive file we need to replace "c" option with "x". Here we extract archivewithtar/dmesg to a restore/ directory.
tar xvf linuxtarfile.tar archivewithtar/dmesg
Extract entire contents of the tar archive linuxtarfile.tar file to restore/ directory:
tar xvf ../linuxtarfile.tar
Linux tar archive command does not use compression by default. Most commonly used compression types with tar command are -z ( gzip ) and -j ( bzip2 ). Create compressed archive of archivewithtar/ with gzip and bzip2:
tar czf linuxtarfile.tar.gz archivewithtar/ tar cjf linuxtarfile.tar.bz2 archivewithtar/
Compare the size of all three tar archives files: no compression, gzip and bzip2:
The approach to extract files from compressed tar archive is the same as to extract data from non-compressed tar archive. Here we extract files from gzip compressed tar archive file:
tar xvzf linuxtarfile.tar.gz
Linux tar archive command can create simple incremental or differential backup with -N or --newer option. Let's consider two files in our archivewithtar/ which have different change time:
Use tar to archive files newer than "7.2.2008 13:54:00" ( make sure to you use correct tar date format !):
tar cvf linuxtarfile.tar --newer "2008-02-07 13:54:00" archivewithtar/
Tar command in interactive mode gives us an option which files to extract:
tar xvjwf linuxtarfile.tar.bz2
Use -p ( --preserve-permissions )tar option to preserve permissions file permission:
tar czfp linuxtarfile.tar.gz archivewithtar/