In this article, we want to discuss how to using tar command on Linux server. You should know, Tar command work on all Linux standard Gnu distribution like CentOS, Ubuntu, Debian and …
Tar provides a standard interface for creating archives and compressing files on Linux systems. this utility takes a large number of files, save them together in an archive, and compress the archive to save space. However, tar provides a multitude of features and options that can lead to hard-to-read commands and make even the simplest operations confusing.
Table of Contents
using tar command on Linux
Extract archive file with Tar
The complexity of tar does not derive from its basic form, but rather from the number of options and settings that you can use to create and interact with archives. Given the tar file ~/archive.tar, the following command can be used to extract the contents of this file into the current directory.
tar -xf ~/archive.tar
This will extract (-x) the archive specified by the file (-f) named on the prompt.
Compress and archive files with Tar
To create an archive of all the files in the directory ~/data, use the following command.
tar -c ~/data > data-archive.tar
By default, tar sends the contents of the archive file to the standard output, you can use this to further process the archive you create. You may choose to bypass the standard output functionality with the -f option. The following command is equivalent to the previous command.
tar -cf data_archive.tar ~/data
You may also add a -v option to increase the verbosity of some commands. For instance the following command will output a list of files as they are added to the archive.
tar -cvf data_archive.tar ~/data
The order of options is sometimes important. The -f option needs to be the last option, and thus appear closest to the name of the file that it specifies. Therefore -cvf will perform as expected while -cfv will fail.
Now we show you some example for tar command.
Tar command Example for archive and compressing files
Creating an Archive
create a file without using the standard output redirection.
tar -cf data_archive.tar ~/data
Compress an Archive using Gzip
create a file with tar and gzip command.
tar -czf ~/data-archive.tar.gz ~/data/
create a tar file with bzip compression.
tar -cjf ~/data-archive.tar.bz2 ~/data/
create a tar file and xzip compression.
tar -cJf ~/data-archive.tar.xz ~/data/
Tar command Example for uncompressed and extracting files
To extract files from a tar archive, issue the following command.
tar -xf ~/data-archive.tar
if you want to extract a gzip file you can use below command.
tar -xzvf ~/data-archive.tar.gz
also for uncompressed and extract files with bz2 files, you can use this command.
tar -xcvi ~/data-archive.tar.bz2
Conclusion
Tar command is the most powerful command for compress, archive and extract compress file on Linux, in this article we show you for using tar command on Linux with a simple way, for more information about tar command please go to GNU tar.