Tar Archiver for Beginners : How to use tar to Archive Files/Directories in Linux/Unix Based Systems

Tar is a command line tool for Archiving files and directories. It is a widely used command line tool to create backup archives (collection of files and directories). Tar stands for Tape Archive, and sometimes referred to as tarball. The tar archive have as extension of '.Tar'.

The TAR file format is common in Linux and Unix systems, but only for storing data, not compressing it. By default, a tar archive file contains uncompressed files, but tar files are often compressed after being created. Together with gzip and bzip2, you can create a compressed archive.

Now in this post we will see examples of some tar commands and how to use these commands.

The tar utility is installed on most Linux systems by default. But in case, if it is not installed on your system then use the below command to install it :
 $ sudo apt-get update
 $ sudo apt-get install tar
The basic format of tar command is as follows :
 $ tar option(s) [archivename] [filename(s)]
These are some basic options :
  • c - Create an archive
  • x - extract files from archive
  • v - verbose mode
  • f - the filename in the next argument

1. To create a tar archive

To create a tar archive of files, use below command.
 $ tar -cvf archivename.tar file1 file2 file3
for example :
 $ tar -cvf archived_files.tar doc1.txt doc2.txt doc3.txt
The above command will create an archived file and named it archived_files.tar. And to archive the files and folders from whole directory, use the below command :
 $ tar -cvf archivename.tar [directory-name]
example :
 $ tar -cvf archive.tar documents/
or
 $ tar -cvf archive.tar /home/user/docs/
2. Extract tar archive :

To extract files from tar archive, use the below command :
 $ tar -xvf [archived-file-name]
example :
 $ tar -xvf archived_files.tar
3. To extract files on a particular directory :

In order to extract files on a particular directory, use the below command :
 $ tar -xvf [archived-file-name] -C [directory-path]
example :
 $ tar -xvf archived_files.tar -C /home/user/docs/
4. List the content of an archived tar file :

With -t option we can list all the files of an archived tar file :
 $ tar -tvf [archived-file-name]
example :
 $ tar -xvf archived_files.tar
5. Verify a Tar archive :

With W option we can verify a tar archive.
 $ tar -tvfW archived_file.tar
Note : we can only verify tar archives, but not the compressed ones (means tar.gz, tar.bz2)

6. Extract only a single file from archive :

To extract a single file from tar archive, use the below command :
 $ tar -xvf [archived-file-name] [file-name-to-be-extract]
example :
 $ tar -xvf archived_file.tar doc.txt
7. Adding/Appending files or directories in tar archive file [r] :

With -r option we can add a file or directory in tar archive.
 $ tar -rvf [archived-file-name] [file-name-to-be-add]
example :
 $ tar -rvf archived_files.tar new_doc.txt
8. Exclude/Remove certain file/folder form tar archive :

To remove a file or folder from tar archive, use the below command :
 $ tar --exclude="file_path_to_be_exclude" -cvf [archived_file_name] [path_to_extract_files]
Now at here first it will extract all the file from archive, then re-archived it and exclude the file which we want to remove. For example :
 $ tar --exclude="/home/user/doc.txt" -cvf documents.tar /home/user/

Compressing Tar Archive :


Now at here we see, how we can compress tar archived. Note that we can also compressed a tar archive file with gzip or bzip2, but with tar we can also create a compressed tar archive directly :

Tar archive with Gzip :

To create a tar archive that is then compressed with the gzip utility, we can pass the -z option, which indicates that we want to use gzip compression on top of the archive. The extension of compressed archived would be tar.gz or tgz.
 $ tar -czvf [compressed_archived_name] [files/folders]
example :
 $ tar -czvf arvhived_file.tar.gz documents/
or
 $ tar -czvf arvhived_file.tgz documents/
To list the contents of archived file, use below command :
 $ tar -tzvf arvhived_file.tgz
To extract the files from compressed archive use the below command :
 $ tar -xzvf arvhived_file.tgz

Tar archive with bzip2

To create a compressed tar archive with the bzip2 utility, we can pass the -j option, which indicates that we want to use bzip2 compression on top of the archive. The extension of compressed archived would be tar.bz2 or tbz.
 $ tar -cjvf [compressed_archived_name] [files/folders]
exmple :
 $ tar -cjvf arvhived_file.tar.bz2 documents/
or
 $ tar -cjvf arvhived_file.tbz documents/
To list the contents of archived file, use below command :
 $ tar -tjvf arvhived_file.tbz
To extract the files from compressed archive use the below command :
 $ tar -xjvf arvhived_file.tbz

Tar archive with xz

To create a compressed tar archive with the xz utility, we can pass the -J option, which indicates that we want to use xz compression on top of the archive. The extension of compressed archived would be .tar.xz.
 $ tar -cJvf [compressed_archived_name] [files/folders]
example :
 $ tar -cJvf arvhived_file.tar.xz documents/
or
 $ tar -cJvf arvhived_file.tar.xz documents/
To list the contents of archived file, use below command :
 $ tar -tJvf arvhived_file.tar.xz
To extract the files from compressed archive use the below command :
 $ tar -xJvf arvhived_file.tar.xz
For help menu type
 $ tar --help
And for manual page type
 $ man type


Conclusion :

The tar utility is very powerful tool archiving files and directories. In this post we saw some usage of tar command with examples. For more information about tar you can check mannual page at here : https://linux.die.net/man/1/tar