The tar (tape archiver) tool is the most commonly used application for data backups on Linux systems. It archives files in a special format, either directly on a backup media (such as tape) or to an archive file in the file system.
The tar Command
The tar command stores, lists, or extracts files in an archive.
$ tar functions archivefile filenames
The output of using a tar command is a tar file. The default output location for a tar file in Linux is the stdout.
The tar Command Options
The table describes some of the commonly used tar command options. For a detailed explanation of the tar command and its options, read the tar man page.
Options | Description |
---|---|
c | Creates a new tar file |
t | Lists the table of contents of the tar file |
x | Lists the table of contents of the tar file |
f | Specifies the archive file or tape device. |
v | Specifies the archive file or tape device. |
h | Follows symbolic links as standard files or directories |
z | Follows symbolic links as standard files or directories |
j | Compresses and extracts files and directories using bzip |
Creating a tar Archive
You can use the tar command to create an archive file containing multiple files or directories onto a disk or file. The following example shows you how to archive your home directory onto a disk.
$ tar cvf user_home.tar /home/user /home/user/ /home/user/.bash_logout /home/user/.bash_profile /home/user/.bashrc ...
The following example shows you how to archive multiple files into an archive file called test.tar.
# tar cvf test.tar test test1 test2 test test1 test2
Viewing a tar Archive
You can view the names of all the files that have been written directly to a disk or file archive. To view the content of the test.tar archive file, enter the following command:
# tar tf test.tar test test1 test2
Extracting a tar Archive
You can retrieve or extract the contents of an archive that was written directly to a disk device or to a file. To extract files from the test.tar archive file, enter the following command:
# tar xvf test.tar test test1 test2