
The tar command in Linux enables archiving and compression of files and directories. It's also known as tape archive (tar) and combines multiple files into a single archive file with the .tar extension that can be compressed to save disk space to simplify file transfers and backups.
This article explains how to use the tar command in Linux to perform archiving and file compression tasks.
tar Command SyntaxBelow is the basic tar command syntax:
Within the above command, [options] modifies the tar command behavior, [archive-file] specifies the archive filename, and [file/directory] specifies the files or directories to include in the archive.
tar Command Optionstar CommandCreate a new tar archive.
The above command creates a new archive.tar that contains the file file1.txt, file2.txt, and all contents in directory/.
Output:
Create a new compressed tar archive using Gzip.
The above command creates a new archive.tar.gz and enables compression using Gzip.
Output:
Extract files from a tar archive.
The above command extracts all files from the archive.tar file into your working directory.
Output:
Extract files from a compressed tar archive.
The above command extracts all files from the compresses archive.tar.gz file to the working directory.
Output:
List all contents in a tar archive.
The above command lists all contents in the archive.tar file without extraction.
Output:
Create a compressed tar archive using Bzip.
The above command creates a new compressed archive.tar.bz2 file using the bzip2 utility.
Output:
Extract files from a Bzip tar archive.
The above command extracts all files from the archive.tar.bz2 to the your working directory.
Output:
Create a new tar archive using the xzipped compression format.
The above command creates a new compressed archive.tar.xz file using xz.
Output:
Extract files from an xzipped archive file.
The above command extracts files from the archive.tar.xz file to your working directory.
Output:
Add new files to an existing tar archive.
The above command appends the file newfile to the existing tar archive archive.tar.
Output:
Update files in an existing tar archive.
The above command updates the updatedfile file in the archive.tar archive if its newer than the existing version.
Output:
Exclude files and directories from tar archive.
The above command creates a new archive.tar file that includes all files except .log files in directory/.
Output:
Compress and archive files to a remote location.
The above command compresses directory/ and transfers it to remotehost using SSH.
Output:
Extract a specific file from a tar archive.
The above command extracts path/to/file from archive.tar.
Output:
Verify the integrity of a tar archive.
The above command lists all contents of archive.tar and discards the output to verify the file integrity.
Use tar with find to archive specific files.
The above command finds all .txt files and archives them to the archive.tar file.
Output:
Extract files to a specific directory.
The above command extracts files from archive.tar to /path/to/directory/.
Output:
Create a new tar archive split into multiple volumes
The above command creates a new tar archive using files from directory/ and splits it into multiple volumes of 5 MB for each file.
Output:
You have used the tar command to archive and compress files, manage backups, and optimize storage on your system. For more command options, run the man tar command to view the tar manual page.
0 Comments
Be the first to comment and share your perspective with the community.