Mastering the `tar` Command in Linux
The tar
command in Linux is a crucial tool for managing file archives. It is used to create, extract, and manipulate archive files, often referred to as tarballs. The name “tar” stands for tape archive, and although it originated as a tool for managing data on magnetic tape, it is now widely used for creating compressed and uncompressed file archives.
Here is a detailed explanation of the tar
command, its options, and usage scenarios.
Overview of ‘tar'
The tar
command creates a single archive file from multiple files and directories, preserving their structure. It can also compress the archive using utilities like gzip or bzip2.
Basic Syntax:
tar [options] [archive-file] [file or directory to be archived]
- Options: Various flags that control the behavior of the
tar
command. - archive-file: The name of the output or input archive.
- file or directory: The files or directories you want to include in the archive.
Common Use Cases
1. Creating a Tar Archive
To create a .tar
archive (without compression):
tar -cvf archive-name.tar file1 file2 directory1
-c
: Creates a new archive.-v
: Verbosely shows progress in the terminal (optional, to see which files are being archived).-f
: Specifies the archive file name (archive-name.tar
).
Example:
tar -cvf mybackup.tar /home/user/Documents
This creates an archive called mybackup.tar
containing all the files and subdirectories in the /home/user/Documents
directory.
2. Extracting Files from a Tar Archive
To extract the contents of a .tar
archive:
tar -xvf archive-name.tar
-x
: Extracts files from the archive.-v
: Verbose output (optional).-f
: Specifies the archive file to extract.
Example:
tar -xvf mybackup.tar
This extracts all the files and directories from mybackup.tar
into the current directory.
3. Creating a Compressed Archive
tar
supports compression using different algorithms, such as gzip, bzip2, and xz. These are often used for reducing the size of the archive.
a) Gzip Compression (.tar.gz
or .tgz
)
tar -cvzf archive-name.tar.gz file1 file2 directory1
-z
: Compresses the archive with gzip.
Example:
tar -cvzf mybackup.tar.gz /home/user/Documents
This creates a gzip-compressed archive called mybackup.tar.gz
.
b) Bzip2 Compression (.tar.bz2
)
tar -cvjf archive-name.tar.bz2 file1 file2 directory1
-j
: Compresses the archive with bzip2, which typically provides better compression than gzip but may be slower.
Example:
tar -cvjf mybackup.tar.bz2 /home/user/Documents
c) XZ Compression (.tar.xz
)
tar -cvJf archive-name.tar.xz file1 file2 directory1
-J
: Compresses the archive with xz, which usually provides better compression than both gzip and bzip2.
Example:
tar -cvJf mybackup.tar.xz /home/user/Documents
4. Extracting Compressed Archives
- For gzip-compressed archives (
.tar.gz
or.tgz
):
tar -xvzf archive-name.tar.gz
- For bzip2-compressed archives (
.tar.bz2
):
tar -xvjf archive-name.tar.bz2
- For xz-compressed archives (
.tar.xz
):
tar -xvJf archive-name.tar.xz
5. Listing Contents of an Archive
To view the files contained in a .tar
archive without extracting them:
tar -tvf archive-name.tar
-t
: Lists the contents of the archive.
Example:
tar -tvf mybackup.tar.gz
This shows a list of all files in mybackup.tar.gz
.
6. Appending Files to an Existing Archive
To add more files or directories to an existing archive:
tar -rvf archive-name.tar newfile newdirectory
-r
: Appends files to an existing archive.
Example:
tar -rvf mybackup.tar newfile.txt
7. Removing Files from an Archive
Removing a file from a tar archive is not as straightforward as adding one. However, you can create a new archive without the file you want to exclude:
tar --delete -f archive-name.tar file_to_remove
--delete
: Deletes a specified file from the archive.
8. Extracting Specific Files from an Archive
To extract specific files or directories from an archive:
tar -xvf archive-name.tar file1 file2
Example:
tar -xvf mybackup.tar documents/report.txt
9. Archiving Files with Permissions
One of the advantages of tar
is that it preserves file permissions, ownership, and timestamps. This is important when backing up files or transferring them between systems. By default, these attributes are preserved when creating and extracting tar archives.
10. Excluding Files from the Archive
To exclude certain files or directories from being archived:
tar -cvf archive-name.tar --exclude='*.log' /home/user/Documents
--exclude
: Excludes files that match the pattern (e.g.,*.log
will exclude all.log
files).
Example:
tar -cvf backup.tar --exclude='*.tmp' /home/user/Documents
This command will exclude all temporary (.tmp
) files from the /home/user/Documents
directory while creating the backup.tar
archive.
11. Using Wildcards
You can use wildcards with tar
to match multiple files or directories based on patterns.
Example:
tar -cvf archive-name.tar *.txt
This will include all .txt
files in the current directory.
Useful Options:
-C
: Change directory before performing the operation. Example:
tar -xvf archive-name.tar -C /target/directory
This extracts the files into /target/directory
.
--strip-components=NUMBER
: Remove a specific number of leading directories from file names. Example:
tar -xvf archive-name.tar --strip-components=2
This removes the first two directory levels when extracting.
--same-owner
: When extracting files, preserve the original ownership of the files. Example:
tar -xvf archive-name.tar --same-owner
-p
: Preserve the file permissions when extracting. Example:
tar -xvpf archive-name.tar
Practical Examples:
- Create a Backup of a Directory:
tar -cvzf backup-home.tar.gz /home/user
This command compresses and archives the /home/user
directory into backup-home.tar.gz
.
- Extract a Backup into a Different Directory:
tar -xvzf backup-home.tar.gz -C /tmp/restore
This command extracts the contents of backup-home.tar.gz
into /tmp/restore
.
- View Contents of a Compressed Archive:
tar -tvzf backup-home.tar.gz
This command lists the contents of the backup-home.tar.gz
archive.
- Archive Files While Excluding Certain Files:
tar --exclude='*.log' --exclude='cache/' -cvzf archive.tar.gz /var/log
This creates an archive of the /var/log
directory but excludes all .log
files and the cache/
directory.
Conclusion:
The tar
command is an essential tool for file archiving and compression in Linux. It allows you to create and extract archives, preserve file permissions and metadata, and compress data using various compression algorithms. With its extensive options, it can handle everything from simple backups to complex, selective extractions.