1. Name

tar [man page] - The GNU version of the tar archiving utility

2. Synopsis

tar  [ - ] A --catenate --concatenate | c --create 
| d --diff --compare | --delete | r --append
| t --list | u --update | x --extract --get [ options ]
pathname [ pathname ... ]

3. 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

4. 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: archive directory with tar
Create an archive to linuxtarfile.tar file of the archivewithtar/ directory:

tar cf linuxtarfile.tar archivewithtar/ 

archive directory with tar to a file
To see what files are being archived by tar command we can use - v ( tar verbose mode ):

tar cf linuxtarfile.tar archivewithtar/ 

tar archive with verbose mode
To instruct tar command to list all files within a archive file we can use command:

tar tf linuxtarfile.tar 

make tar to list all files within an archive
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 

tar - extract just particular file
Extract entire contents of the tar archive linuxtarfile.tar file to restore/ directory:

tar xvf ../linuxtarfile.tar 

Extract entire contents of the tar archive
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: create compressed archive with tar
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 

extract compressed archive with tar
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:

simple tar incremental or differential backup
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/  

simple tar incremental or differential backup newer
Tar command in interactive mode gives us an option which files to extract:

tar xvjwf linuxtarfile.tar.bz2 

interactive tar to extract particular files
Use -p ( --preserve-permissions )tar option to preserve permissions file permission:

tar czfp linuxtarfile.tar.gz archivewithtar/ 

Free Linux eBooks

Go to top