The tar in Linux is a commonly used lightweight command line tool for creating file archives and compressing them. Not only for archiving, but users can also use it for extracting, and manipulating existing archives as well.
The name “tar” derived from “tape archive” because the command was originally designed to use with Tape backup devices. However, being a powerful command line tool it remains to be an important system administration tool even today. Although on most Linux systems Tar tool comes by default, if it is not then can be installed easily using the default package manager and system repository.
How does the tar Command Work?
Like any other tool used for archiving files, Tar also, when it runs, combines all files present in a folder to create a single Archive file. Well, the archive can contain a single or more files depending upon what the user wants. However, the reason for compressing files could be any but as we compressed the files they become easy to transfer and also consume less space on a hard drive.
When using tar, you can specify what types of files should be put in the archive (e.g., all, pdf, documents), which directories should be included (e.g., all subdirectories), etc… You can also use this utility to compress large text, image, executable and other types of files — making them easier to store or transfer over the internet.
How to install Tar on Linux distros?
As I mentioned earlier, most of the Linux systems by default come with a pre-included Tar package to use. However, if your system doesn’t have the Tar, then here are the commands to follow:
For Debian, Ubuntu, Linux Mint, and more…
sudo apt install tar
For RedHat, Alamlinux, Rocky Linux, Oracle Linux, Amazon Linux, Fedora, CentOS, and other similar Linux systems:
sudo dnf install tar
For Arch or Manajro Linux
sudo pacman -Syu tar
Using tar with Arguments
When executing tar on UNIX/Linux systems there are usually two arguments: action and target filename(s). Depending on which argument you pass in first, determines how you would use it—for instance, the current working directory or specific path that was passed as an argument instead of using a relative path(s).
As we know the main function of ‘tar‘ is to create archives that group multiple files or directories into a single compressed file (called an “archive”). You can use ‘tar’ to create two kinds of archives: ones that are compressed with gzip or bzip2 compression algorithms (which usually have the .gz/.bz2 extension) or uncompressed “regular” tarball archives (.tar).
Here are some of the common uses of the tar
command:
1. Creating a tar archive without compression
To create a tar archive of a set of files or directories, you can use the following command:
tar -cvf my.tar file-name
my.tar is the name of the output archive in which we want to save the file. Whereas, replace “file-name” with the file in your current directory that you want to be archived.
For example, if there is a pdf file, let’s say demo.pdf then to archive it, the command will be:
tar -cvf my.tar demo.pdf
In the same way, if you want to add multiple files present in your current directory, then you just need to mention their full name. Similarly, we can even Tar a folder and all files available in it, as well.
tar -cvf my.tar demo.pdf image.jpg my.pdf
Command Explanation:
- tar – It is a command line tool for Archiving
- cvf – In this, -c means “Creates a new archive”; -v is to show the process of archiving performed by the tar; -f is to specify the file.
2. Extracting files from a tar archive
Those who already have some Tar file and want to extract the content from that tar archive, can use the following command:
tar -xvf my.tar
Replace the my.tar with the actual file name.This will extract all the files and directories from it into the current directory. Option -x
is for extracting the file.
3. Listing the contents of a tar archive
Well, before extracting the content of an archive file, you can view a list of files and directories present inside a tar archive, using the following command:
tar -tvf my.tar
This will display a list of all the files and directories in the my.tar
file.
4. Compressing a tar archive
Now coming to the compressing part, if you want to use the Tar command to compress a file then we can use gzip compression. For that nothing special we need to do, just add .gz
at the end of the tar in the command.
Syntax:
tar -czvf my.tar.gz file-name
This will create a new file called my.tar.gz
using gzip compression. In the above syntax -z
option is to read or write compression in gzip format.
5. Extracting a compressed tar archive
To extract files from a compressed tar archive, we can use the following command:
tar -xzvf my.tar.gz
This will extract all the files and directories from the my.tar.gz
file into your current directory.
- -x– For extracting
- -z For reading to writing compressed file
- -v – For giving output on the terminal of the extracting or archiving process
- -f – For specifying the file
Note: The options used with the tar
command can vary depending on the specific use case and the version of Linux being used. You can find more information about the tar
command by typing man tar
in the terminal. Alternatively you can use:
tar --help
Other Articles: