Linux tar Command Tutorial with Examples

Linux tar command is used to create archives for data, files, and folders. The tar command name comes from tape archive . One of the most useful features of the tar command is compressing multiple files and folders into a single file which can be easily compressed with different tools like gz, bz, 7z etc. In this tutorial, we examine how to use the tar command for different cases and scenarios.

tar Command Syntax

The tar command has the following syntax.

tar OPTIONS TAR_FILE FILE
  • OPTIONS are used to take different action with tar command.
  • TAR_FILE is the tar file which will be used to created.
  • FILE is single or more files and folders to tar or extract.

tar Options

The tar command has a lot of options but some of them are very popular. We list the popular options below.

OptionDescription
-cCompres Create tar Archive
-xExtract tar Archive
-fCreate tar Archive with the provided name
-tDisplay files inside the tar archive
-uAdd a new file to an existing tar archive
-vDisplay verbose information
-AConcatane archive fişles
-zCreate *.tar.gz file
-jFilter archive tar file using tbzip
-WVerify tar archive file
-rUpdate or Add file or directory into an existing tar archive

Create Tar File

The tar command uses the cvf option in order to create a tar archive file. In the following example we will create the tar archive of the files with *.txt extension.

c

Create Tar File for Directory

The tar command can be also used to create a tar archive from a single specified directory. Similar to the previous example the cvf options are used to create a tar archive of a single directory.

$ tar cvf Downloads.tar Downloads

Create Tar File for Multiple Directories

The tar command can be also used to create tar archive of multiple directories. The directories are added after the tar file name.

$ tar cvf Downloads.tar Downloads1 downloads2 mydownloads

Extract Tar File

The xvf option is used to extract existing tar archive.

$ tar xvf Downlaods.tar

Compress Gzip and Tar

The tar command only archives a file but does not compress it technically. The tar is generally used with compression commands like gzip. The *.tar.gz is used for tarred and gzipped files. The cvzf option is used to archive and compress this archive with gzip algorithm.

$ tar cvzf downlaods.tar.gz downloads

Extract Gzip and Tar

The xvfj option is used to extract existing *.tar.gz compressed archive file.

$ tar cvzf downlaods.tar.gz

In some cases, we may need to get or print the size of the extracted files. We can accomplish this without extracting the archive completely. We will extract the archive and redirect it into wc command in order to count the character count which is around the file size as KB.

$ tar xvf nmap.tar | wc -c

Add New File To Existing Tar File

The rvf option is used to add a new file to the existing tar file. The file name is provided as the last parameter.

$ tar rvf downlaods.tar myfile.txt

List Tar File Contents

The contents of a tar file can be listed with the tf option.

$ tar tf downloads.tar

Grep Tar File Content

Generally, tar archive files contain a lot of files and folders and in order to find a specified file or folder, the grep command can be used to filter a list of files and folders. In the following example, we search or filter for files or folders with the name of data .

$ tar tf downloads.tar | grep "data"

Leave a Comment