translations: [ es/Español ] · [ fr/Français ] · [ de/Deutsch ]
Table of contents
The tar
command in Linux is a powerful utility for creating, extracting, compressing, and managing archives. It offers various options and functionalities to handle files and directories.
Archiving and Extracting
Creating an Archive
To create an archive, use -cvf
followed by the archive name and files/directories to be archived. The -c
flag creates a new archive, -v
shows verbose output, and -f
specifies the archive file name.
Example:
tar -cvf archive.tar file1 file2 directory1
This command creates a new archive named archive.tar
containing file1
, file2
, and directory1
.
Extracting an Archive
To extract files from an archive, use -xvf
followed by the archive name. The -x
flag extracts files from an archive, -v
shows verbose output, and -f
specifies the archive file name.
Example:
tar -xvf archive.tar
This command extracts the contents of archive.tar
in the current directory or creates a new directory if the archive contains multiple files.
Viewing Archive Contents
To view the contents of an archive, use -tvf
. The -t
flag displays the contents of an archive, -v
shows verbose output, and -f
specifies the archive file name. This command doesn’t extract files but lists the contents of the archive.
Example:
tar -tvf archive.tar
This command shows the contents of the archive.tar
file without extracting its contents.
Compression and Decompression
Compressing an Archive (gzip)
To compress an archive using gzip compression, use -zcvf
. The -z
flag compresses files using gzip, -c
creates a new archive, -v
shows verbose output, and -f
specifies the archive file name.
Example:
tar -zcvf archive.tar.gz file1 file2 directory1
This command compresses file1
, file2
, and directory1
into an archive named archive.tar.gz
using gzip compression.
Decompressing an Archive (gzip)
To decompress a gzip-compressed archive, use -zxvf
. The -z
flag decompresses files using gzip, -x
extracts files from an archive, -v
shows verbose output, and -f
specifies the archive file name.
Example:
tar -zxvf archive.tar.gz
This command decompresses and extracts the contents of the archive.tar.gz
file.
Additional Functionalities
Testing Archive Integrity
To test the integrity of an archive, use -t
. The -t
flag checks the integrity of an archive without extracting its contents. It verifies if the archive is corrupt or intact.
Example:
tar -tvf archive.tar
This command tests the integrity of the archive.tar
file.
Recursive Compression
To recursively compress a directory, use -zcvf
. The -z
flag compresses files using gzip, -c
creates a new archive, -v
shows verbose output, and -f
specifies the archive file name.
Example:
tar -zcvf archive.tar.gz directory1/
This command recursively compresses directory1/
and its contents into an archive named archive.tar.gz
.
Exclude Files/Directories
To exclude specific files or directories while creating an archive, use --exclude
. This option allows excluding certain files or directories from being archived.
Example:
tar -cvf archive.tar --exclude='file1' directory1/
This command creates an archive named archive.tar
while excluding file1
from directory1/
.
Incremental Backup
To perform an incremental backup, use --listed-incremental
. This allows creating an incremental backup, which only adds new or modified files since the last backup.
Example 1:
tar -cvf backup1.tar --listed-incremental=backup.snar directory1/
This command creates an initial full backup named backup1.tar
and generates a snapshot file named backup.snar
.
Example 2:
tar -cvf backup2.tar --listed-incremental=backup.snar directory2/
Here, backup2.tar
is an incremental backup based on the changes since the last backup, utilizing the snapshot file backup.snar
.
Example 3:
tar -cvf backup3.tar --listed-incremental=backup.snar directory3/
Similarly, backup3.tar
represents an incremental backup with changes relative to the previous backup, leveraging the existing snapshot file backup.snar
.
The tar
command offers a wide range of functionalities, making it a valuable tool for managing files and directories in Linux systems.
Created on: Jan 7, 2024
Discover More within this Subject:
- Ways to Check Linux Distribution and Kernel Version
- Understanding `kill` Command in Linux
- Understanding `ps` Command in Linux
- Essential Linux Network Commands for Webmasters
- Comperhensive Guide to Gzip in Linux