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:
tar [options] [file or directory]
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
tar -cvf archive.tar file1 file2
Creates a tarball named “archive.tar” containing “file1” and “file2”.
Extract a Tarball
tar -xvf archive.tar
Extracts all files from the tarball “archive.tar”.
List Files in a Tarball
tar -tvf archive.tar
Lists all files included in the tarball “archive.tar”.
Compress with Gzip
tar -czvf archive.tar.gz file1 file2
Creates a tarball named “archive.tar.gz” with gzip compression.
Extract Gzipped Tarball
tar -xzvf archive.tar.gz
Extracts files from a gzipped tarball “archive.tar.gz”.
Create a Tarball with Compression
tar -cvzf archive.tar.gz folder
Creates a tarball “archive.tar.gz” of a folder with gzip compression.
Extract Specific Files from Tarball
tar -xvf archive.tar file1
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:
tar -cvf archive.tar /path/to/directory
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:
tar -xvf archive.tar -C /path/to/extract
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:
tar -zcvf archive.tar.gz /path/to/directory
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:
tar -tvf archive.tar
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:
tar -xvf archive.tar path/to/file
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:
tar -uvf archive.tar newfile.txt
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