How to Use tar Command in Linux with examples complete Guide 2021

tar command in linux

How to Use tar Command in Linux

Objective

The objective of this article is to explain various steps of creating backup data of a system using various compression and encryption method, which might be helpful to the reader to go through about tar command and become an expert to use it as an Administrator. We will discuss the following:-

back to top

Understanding tar Command in Linux

Role of System Administrator is very important in terms of use of tar Command in Linux to obtain regular automated backup of system or server. Tape archive (tar) process actually uses following types of format for archiving data :-

Archive Type Explaination
tar (.tar) Convert one or more folders or files into a simple uncompressed tar file with .tar extension, may be known as archive file.
gzip (.tar.gz) This command is used with tar command to make an medium compressed archive file with .tar.gz extension.
bzip (.tar.bz2) This command is used with tar command to make highly compressed archive file with with .tar.bz2 extension.
xzip (.tar.xz) This command is used with tar command to make a super compressed archive file with .tar.xz extension.
tar Command in Linux – Features of tar Command

back to top

Advantages of Use of tar Command in Linux

tar Command in Linux – Features of tar Command

Use of tar Command in Linux is almost compulsory for each administrator in view of safeguard and economic storage of archive data of a particular system. The prominent features of tar command in Linux are :-

  • Portability of Data: Actually, by use of tar command, we can simply combine all the required data kept in various folders, sub folders and files, into a single file. Instead of sending many folders and files separately, sending of a bundle is easier. Hence, this single tar file can be sent anywhere through the online or offline process very easily.
  • Organized Backup (Archive) Data: System administrators are usually taking backup of their system periodically, in view of the safety of the system. In case of any incident of theft or damage of the system, correct backup data can recover the system easily. Hence, there is a need to organize all these periodic backup, so that these can be utilized easily at any time. Therefore, tar command is very useful to combine all the backup in a single file and that can be kept in a safe place with proper indexing.
  • Compression: When we required backup of a large number of data, the archive file may take much space within the backup hardware, i.e., tape archive box, disc storage medium, etc. Compression can able to compress all these archive data in a specific size and that may take little storage space in any medium.
  • Encryption: Encryption is also possible with tar command in view of the security of backup data. It will be more difficult to read or edit backup data by any unauthorized person if encryption is used with tar command during obtaining the backup.

  back to top

Use of tar Command Options in Linux

Options Explaination
-c To create an archive file.
-x To extract content of an archive file.
-v To display the progress of archiving process.
-f To give file name of archive file being created.
-j To compress archive file through bzip2, being created.
-z To compress archive file through gzip, being created.
-r Add or updated content in an archive file already created earlier.
-W To verify an archive file being created.
-wildcards To specify extract pattern of tar command in Linux.

  back to top

Creating Archive File using tar Command

Creating Simple tar File

tar -cvf 2020-05-21_brother.tar /mohan/brother

tar Command in Linux – Creating Simple Tar File

Description : This command is used to create a simple tar file of the whole directory “brother” kept in the present working directory “mohan”. The newly created file “2020-05-21_brother.tar” will be created at the present working directory i.e., “mohan”. The name of backup file given with consideration of proper indexing in the format of “YYYY-MM-DD_Directory_Name.extension”. This kind of name will be arranged sequentially in the storage medium and the same will be easy to find.

Creating a Compressed gzip and bzip Archive File

tar -cvzf 2020-05-21_brother.tar.gz /mohan/brother

tar Command in Linux – Creation of Compressed Archive File

Description : This command is used to create a gzip compressed tar file of the whole directory “brother” kept in the present working directory “mohan”. In the above command, the extension .tgz may also be used in place of .tar.gz and the same output will be given. For bzip, we have to use “-cvjf” in place of “-cvzf” and extension should be used as .tar.bz2.

back to top

How to Check Size of an Archive File

File Size

ls -l sister

tar Command in Linux – Watching File Size

Description : Size of file will be shown for each files kept in a directory.

Directory Size

du mohan

tar Command in Linux – Watching Size of a Folder

Description : Size of all the folders and self directory will be shown.

du -s mohan
du -sh mohan

tar Command in Linux – Watching Size in KB and MB

Description : Size of folder will be shown in KB and MB (short unit) respectively.

back to top

Display List of Files and Folders kept in Archive Backup File

tar -tvf 2020-05-21_brother.tar

tar -tvf 2020-05-21_brother.tar.gz

tar Command in Linux – Display List of Files and Folders in Archive File

Description : The long format of list of all the files or folders of the archive file having .tar, .tar.gz or .tar.bz2 will be displayed with the same command. Only make sure to provide correct archive file name with proper extension code.

back to top

Extract Content of an Archive Backup File

tar -xvf 2020-05-21_brother.tar -C /mohan/backup

tar Command in Linux – Extract Content of an Archive File

Description : All the files and folder of the archive file will be extracted in the given directory, as mentioned in diagram. For .tar.gz or .tar.bz2 the extension of the archive file should be changed accordingly.

back to top

Extract a Specific File from an Archive Backup File

Extract from Simple tar File

tar -xvf back1.tar sister/profile.doc

tar Command in Linux – Extract a Specific File from a tar File

Description : The single file “profile.doc” will be extracted in a new folder “sister”.

Extract from a Compressed .gz file

tar -xvzf backself.tar.gz self/biodata

tar Command in Linux – Extract a Specific File from a Compressed tar File

Description : The single file “biodata” will be extracted in a new folder “self”.

back to top

Extract More than One Files from an Archive Backup File.

tar -xvf back1.tar sister/profile.doc sister/tax.ppt

tar Command in Linux – Extract Multiple Files from a tar File

Description : The command is used for same as used for extracting single file. Only the files with its path should be given one by one separated with horizontal space. In case of compressed file, the same procedure will be followed.

back to top

Extract Number of Similar Group Files from an Archive Backup File

tar -xvf back1.tar sister/*.doc

tar Command in Linux – Extract Number of Similar Group of File from Tar File

Description : This process will extract all the files having .doc extension in the tar archive file back1.tar. In case of compressed .gz or .bz file, same the procedure will be followed. However, some examples of mentioning group of files are as follows :-

  • *.mp3 : All audio files having extension of mp3, will be taken into consideration.
  • *.xml : All xml files will be considered for extraction.
  • f*.* : All kinds of files having first letter “f” in its name, will be observed.
  • *k.jpeg : All jpeg files having last letter “k” in its name, will be observed.
  • G*.mp* : All those files having first letter “G” in its name and having first two letter “mp” in its extension, will be observed.

back to top

Add One or More Files or Folders in an Existing Archive File already Created Earlier

Adding Single File in a Simple tar File

tar -rvf back1.tar images.txt

tar Command in Linux – Adding Single File in a Tar File

Description : A file “images.txt”, which was kept in the directory “mohan” has been added in the tar file “back1.tar”. In case of compressed tar file like .gz or .bz the process is not possible to add any other file in it.

Adding a Folder or multiple files in a tar File

tar -rvf back1.tar parents .txt v.xls

tar Command in Linux – Adding a Folder and Multiple File in a tar File

Description : A folder “parents”, all the text files and an excel file having first letter “v” in its name have been jointly added in the tar file.

back to top

Encryption of Archive File

Encrypt an Archive File

tar -czf – * | openssl enc -e -aes256 -out

tar Command in Linux – Encrypt an Archive File

Description : The terms used in this command are explained below :-

  • enc : OpenSSL command, which is an open-source command mostly used to setup encryption keys, and SSL/TLS certificates.
  • -e : An enc command, which is basically used to encrypt the given file.
  • -aes256 : It is the method of encryption.
  • -out : This option is used to ask the archive file name.
  • After applying command, password will be asked twice.

Accessing the Encrypted Archive File without Correct Password

tar -xzf backself.tar.gz

tar Command in Linux – Accessing Encrypted Archive File Without Password

Description : If we try to check content of the encrypted compressed file, error will be shown since the file is encrypted with password.

Decrypt the Compressed tar File

openssl enc -d -aes256 -in backself.tar.gz | tar xz -C test

tar Command in Linux – Decryption of a Compressed tar File

Description : The file decrypted after entering password.

back to top

Special Tips – Split an archive File

Firstly you have to check size of your tar or compressed tar file. For this, you can use “du -sh” command or you can also use “ls -lh” command.

Secondly you have to decide that how much size you require for a part of the tar file. After that according to the required size, you can apply following command :-

split -b 1M back1.tar sback.tar.split

split -b 10K backself.tar.gz backselfnew.tar.gz.p

tar Command in Linux – Splitting an Archive File into two or more parts

Description :

  • For Simple tar File : Full size of backup file “back1.tar” was found as 3MB. Since I require 1MB of each part, the command applied as per given example in diagram.
  • For Compressed tar File : Full size of compressed file “backself.tar.gz” was found as 24K. Since I require 10KB of each part, the command applied as per given example in diagram.

Getting Help

tar –help | more

tar Command in Linux – Getting Help

Description : In addition to the above, various other options for use with tar command are available, which are not supposed to be very important and also these are not being used frequently.

back to top

You can also visit at the following links to read more on Linux :-

If you like our content, please consider buying us a coffee.
Thank you for your support!