One of the most performed functions during the day to day activities of a system admin is data archiving or data compression/Decompression. And normally we use the ‘tar’ command to do so but there are other options as well for data archiving or extraction, but that’ the tutorial for another time. In this tutorial, we are going to discuss tar command examples & the various actions we can achieve using them All these commands will work on most of the Linux distributions.
It is certainly the most popular & widely used command for compressing or decompressing files. It has integrated compression & creates an archive with the “.tar” extension & we can then also use compress it with the ‘gzip’ or ‘bzip2’ compression method.
Recommended Read: Learn to use Screen command (with examples)
Also Read: Use of du & df commands (with examples)
Let’s discuss some tar command examples,
Creating a simple archive
To create a simple archive of using tar, the syntax is
$ tar -cvf output.tar /path/input
here, -c will create an archive,
-v, will enable verbose mode & will show the progress,
-f, allows us to name the archive
output.tar will be the name of the created archive, &
/path/input is the complete path to file or directory that needs to be archived.
Creating a compressed GZIP archive
To create a compressed archive with tar, the syntax will be
$ tar -cvzf output.tar.gz /path/input
here, the -z option used with the above-mentioned command will help us create a Gzip compression based archive. Also, notice the change in the output file extension, it will now be ‘.tar.gz’ or ‘tgz’ instead of only ‘tar’.
Creating a compressed Bzip2 based archive
Now for some reason, if we want to create an archive that used Bzip2 as the compression method, then we can use the following syntax to create a compressed archive
$ tar -cvjf output.tgz2 /path/input
Here, the -j option will create a Bzip2 based tar archive & the resulting output file extension will be ‘tgz2’or ‘tar.bz2’
Compressing multiple directories
With tar we can also compress multiple directories at once, syntax for the command will be
$ tar -cvf output.tar /path/input1 /path/input2 /path/input3
Here, input1, input2, input3 are the various directories or files that need to be compressed.
Creating an archive excluding some files/directories
We might need to create an archive or files or directories and might need to exclude some files from being archived. To do that, we can use ‘- - exclude’ option & syntax for the command will be,
$ tar -cvf output.tar /path/input1 -- exclude=/path/exclude_file –exclude=/path/exclude_directory
We use multiple ‘--exclude’ to exclude multiple files & directories, we can also exclude a particular file type,
$ tar -cvf output.tar /path/input1 – exclude=/path/*.txt
Listing of files
We can also list the contents of an archive without extracting it using the following commands,
To view the contents of a simple tar archive, the command is
$ tar -tvf output.tar
To view the content of a gzip tar archive, the command is
$ tar -ztvf output.tar.gz
To view the content of the Bzip2 archive, the command is
$ tar -jtvf output.tar.gz2
Decompressing the archives
If we need to extract or decompress the archives, commands will be,
To extract the contents of a simple tar archive, the command is
$ tar -xvf output.tar
To extract the content of a gzip tar archive, the command is
$ tar -zxvf output.tar.gz
To extract the content of the Bzip2 archive, the command is
$ tar -jxvf output.tar.gz2
That’s it guys, we now end this tutorial on some common Tar command Examples in Linux to compress or decompress files. I hope these tar command examples were informative to you guys, please do leave your valuable comments or suggestions using the comment box below.