How to Compress Files in Linux Using the tar Command?
Managing storage, sending data, and archiving critical files is an important activity you can perform in Linux and that’s why compressing files in Linux is necessary. The tar command is among the most commonly used tools for this purpose. It lets users package multiple files into one single archive as well as supports compression for gzip, bzip2, and xz. tar allows you to compress and extract data, either for backup to system files or to save storage space.
Users can use tar to create compressed archives, which can be in the form of .tar.gz, .tar.bz2, and .tar.xz, each with its own features of different levels of compression and speed. In web server environments, such as Linux, the efficient management of files is essential, and this command is used very commonly. It is used by system administrators and developers to manage logs, backups, as well as setup of the software. The knowledge of how to compress files with tar brings benefits in better resource management and simpler data handling.
In this article, we will see how to use tar command to compress files and directories in Linux effectively. If you are a beginner or an experienced Linux user, you should know tar, because it will considerably improve your file management skills. If you also want to install essential software on a server, have a look at How to Install Perl 5.28 on an Arch Linux Webserver.
Using the tar Command to Compress Files in Linux
tar Command Syntax
$ tar [options] [archive-file] [file/directory]
– `[options]` – Defines the action or behavior of the tar command.
– `[archive-file]` – Specifies the name of the archive.
– `[file/directory]` – Lists the files or directories to be included in the archive.
Common tar Command Options
| Option | Description |
|——–|————-|
| `-c` | Creates a new archive. |
| `-x` | Extracts files from an archive. |
| `-v` | Enables verbose mode to display progress. |
| `-f` | Specifies the archive filename. |
| `-z` | Compresses the archive using gzip. |
| `-j` | Compresses the archive using bzip2. |
| `-J` | Compresses the archive using xz. |
| `-t` | Lists the contents of an archive. |
| `-r` | Appends files to an existing archive. |
| `-u` | Updates files in an existing archive. |
Practical Examples of Using the tar Command
Create a tar Archive
$ tar -cvf archive.tar file1.txt file2.txt directory/
This command creates `archive.tar` containing `file1.txt`, `file2.txt`, and the `directory/` folder.
Create a Compressed Archive Using Gzip
$ tar -czvf archive.tar.gz file1.txt file2.txt directory/
This creates a gzip-compressed archive `archive.tar.gz`.
Extract Files from a tar Archive
$ tar -xvf archive.tar
This extracts all files from `archive.tar` into the current directory.
Extract a Compressed tar Archive
$ tar -xzvf archive.tar.gz
This extracts all files from `archive.tar.gz`.
List Contents of a tar Archive Without Extracting
$ tar -tvf archive.tar
This displays a list of files inside `archive.tar`.
Create a Compressed tar Archive Using Bzip2
$ tar -cjvf archive.tar.bz2 file1.txt file2.txt directory/
This command creates a `bzip2`-compressed archive.
Extract a Bzip2 Compressed Archive
$ tar -xjvf archive.tar.bz2
This extracts all files from `archive.tar.bz2`.
Create an xz Compressed tar Archive
$ tar -cJvf archive.tar.xz file1.txt file2.txt directory/
This creates an `xz`-compressed archive.
Extract an xz Compressed Archive
$ tar -xJvf archive.tar.xz
This extracts files from `archive.tar.xz`.
Append a File to an Existing tar Archive
$ tar -rvf archive.tar newfile.txt
This appends `newfile.txt` to `archive.tar`.
Update a File in an Existing tar Archive
$ tar -uvf archive.tar updatedfile.txt
This updates `updatedfile.txt` inside `archive.tar` if the file is newer than the stored version.
Advanced tar Usage Scenarios
Exclude Specific Files from an Archive
$ tar –exclude=’*.log’ -cvf archive.tar directory/
This creates `archive.tar`, excluding all `.log` files inside `directory/`.
Compress and Transfer Files to a Remote Server
$ tar -czvf – directory/ | ssh user@remotehost “cat > /path/to/destination/archive.tar.gz”
This compresses `directory/` and transfers it to `remotehost` via SSH.
Extract a Specific File from an Archive
$ tar -xvf archive.tar path/to/file.txt
This extracts `path/to/file.txt` from `archive.tar`.
Verify the Integrity of a tar Archive
$ tar -tvf archive.tar > /dev/null
This verifies `archive.tar` without displaying its contents.
Find and Archive Specific Files Using find and tar
$ find . -name “*.txt” -print0 | tar -cvf archive.tar –null -T –
This finds all `.txt` files and archives them into `archive.tar`.
Extract Files to a Specific Directory
$ tar -xvf archive.tar -C /path/to/destination/
This extracts `archive.tar` into `/path/to/destination/`.
Split a Large tar Archive into Multiple Parts
$ tar -cvf – directory/ | split -b 5M – archive_part_
This creates an archive of `directory/` and splits it into 5MB parts.
By mastering the `tar` command, you can efficiently manage large file sets, backups, and software distributions on Linux systems.
Conclusion
If you are a regular user or an expert of Linux, you will know that the tar command is one such essential tool to manage files on Linux that have power archiving and compression in it. tar can be used to backup critical data, move files between machines, or to slim down storage space. It’s really versatile and its ability to plugin with the various compression methods like gzip, bzip2 and xz makes it highly able.
Knowledge and use of the various options offered by tar will lead to more customized strategies of file management around users particular needs. Tar is a go to utility for system administrators and developers alike; from basic archiving to very advanced operations like remote transfer and integrity checks.
Through the examples and techniques discussed in this guide, large scale file operations will feel no hassle. Learning how to use tar will make you more efficient with managing files in Linux, better organizing, securing and accessing your data.