Tar is the most important command for Linux administrator and data backup engineer. Almost every day, we use this command to take the backup of a file or directory, moving data from a server to another, etc.
Tar stands for Tape archive.
Linux tar command most widely used for backup and archive data.
It is essential to archive multiple files and directory into a single file.
Here some examples of tar command which is used by System Administrator every day.
tar [options] [archive-file] [file or directory to be archived]
In this article, we will check how to use the tar command in your favorite Linux machine by using these three examples.
- Use tar command to create ‘tar’ archive file
- Use tar command to create tar.gz archive file
- Extract tar archive file
Use tar command to create ‘tar’ archive file
You can create a tar archive file by using the option “-cvf” as shown below:
# tar -cvf linuxconcept-14-09-19.tar /home/linuxconcept/
The options described as:
c – it is used to create a new “.tar” archive file.
v – it is use to get the output of command verbosely
f – it applies to give the file name for the archive file.
Use tar command to create tar.gz archive file
You can create a gzip archive file with extension tar.gz using the “tar” command with option “z”.
For example, here we create a tar.gz archive file for linuxconcept home directory, as shown below:
# tar cvzf Linuxconcept-14-09-19.tar.gz /home/linuxconcept OR # tar cvzf Linuxconcept-14-09-19.tgz /home/linuxconcept
The files with extension “tar.gz” and “.tgz” are similar.
Extract tar archive file
To extract “tar” file need to use option “x” with tar command, as shown below:
# tar -xvf linuxconcept-14-09-19.tar
If you want to extract files in any specific directory, you can use the option “-C” to specify the directory when a file should extract, as shown below:
# tar -xvf linuxconcept-14-09-19.tar -C /home/linuxconcept/videos/