tar Linux Command Guide
The Linux tar command is a versatile tool used for archiving files and directories. It allows you to create compressed or uncompressed archives, extract files from an archive, and more. With tar, you can easily manage and backup large amounts of data while saving disk space. The command offers various options for customization, including specifying compression levels, adding files to an existing archive, and excluding specific files or directories. Whether you’re a beginner or an experienced user, mastering the tar command is essential for efficiently working with files and directories in a Linux environment.
tar Syntax:
Options:
Option | Description |
---|---|
-c | Create a new archive |
-x | Extract files from an archive |
-f | Specify the file name of the archive |
-v | Verbose mode |
-z | Compress the archive with gzip |
-j | Compress the archive with bzip2 |
-t | List the contents of an archive |
-a | Auto-detect compression format |
Parameters:
Parameter | Description |
---|---|
file or directory | Specifies the file or directory to be archived or extracted |
tar Command Samples:
Create a Tarball
Creates a tarball named “archive.tar” containing “file1” and “file2”.
Extract a Tarball
Extracts all files from the tarball “archive.tar”.
List Files in a Tarball
Lists all files included in the tarball “archive.tar”.
Compress with Gzip
Creates a tarball named “archive.tar.gz” with gzip compression.
Extract Gzipped Tarball
Extracts files from a gzipped tarball “archive.tar.gz”.
Create a Tarball with Compression
Creates a tarball “archive.tar.gz” of a folder with gzip compression.
Extract Specific Files from Tarball
Extracts only “file1” from the tarball “archive.tar”.
How do I create a tar archive of a directory in Linux?
To create a tar archive of a directory in Linux, use the following command:
How do I extract a tar archive in a specific directory in Linux?
To extract a tar archive in a specific directory in Linux, execute the following command:
How do I compress a directory using tar in Linux?
To compress a directory using tar in Linux, use the following command with gzip compression:
How do I list the contents of a tar archive file in Linux?
To list the contents of a tar archive file in Linux, run the following command:
How do I extract a single file from a tar archive in Linux?
To extract a single file from a tar archive in Linux, use the following command:
How do I update a tar archive with new files in Linux?
To update a tar archive with new files in Linux, use the following command:
Applications of the tar command
- Creating a tar archive
- Extracting files from a tar archive
- Adding files to an existing tar archive
- Compressing a tar archive using gzip
- Extracting a compressed tar archive using gzip
- Compressing a tar archive using bzip2
- Extracting a compressed tar archive using bzip2
- List the contents of a tar archive
- Extracting specific files from a tar archive
- Verbose output while creating or extracting a tar archive