15 tar Command Examples for Sysadmin and Developers

tar is without doubt one of the widespread file archiving codecs obtainable in Unix and Linux primarily based methods.

The identify itself is derived from Tmonkey ARchives as a result of it was developed for writing sequential information on tape gadgets. It is usually known as tarball.

By default, tar simply archives the recordsdata with out compression, however utilizing sure components. We are able to use totally different compression methods to get a compressed output. tar utility is normally included by default in most Linux distributions, and the format itself is supported by different working methods, together with Home windows and macOS, by means of numerous instruments and utilities.

On this article, we’ll focus on some frequent examples and the right way to use the tar command and its supported flags.

So let’s begin…

Create a tar archive

To create a easy, uncompressed archive, the syntax for tar command reads:

$ tar cvf <tar-file-name> <files-to-archive>

Flags right here c stands for creation, v for prolonged output and f for specifying the file identify of the tar archive. By conference, specify the tar file identify with .tar extension. Recordsdata to be archived may be specified utilizing wildcard characters or as single or a number of file names/paths.

For instance, I’ve three recordsdata in my folder:

$ ls -l
complete 12
-rw-r--r-- 1 abhisheknair abhisheknair 13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair 19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair 24 Sep 12 20:08 file3.txt
$

And I need one tar archive containing all three recordsdata may be performed as follows:

$ tar cvf archive.tar *
file1.txt
file2.txt
file3.txt
$ ls -l archive.tar
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 12 20:15 archive.tar
$

I may also solely specify particular recordsdata to archive, equivalent to:

$ tar cvf archive1.tar file1.txt file2.txt
file1.txt
file2.txt
$ ls -l archive1.tar
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 12 20:15 archive1.tar
$

Create compressed archive (GZ)

tar permits not solely archiving recordsdata, but in addition compressing them to avoid wasting house. One of many widespread compression codecs is gunzip, normally represented as an extension .gz after .tar or if tgz. We are able to use z flag to point that the recordsdata ought to be compressed utilizing gunzip. Here is an instance:

$ tar cvzf archive.tar.gz file*
file1.txt
file2.txt
file3.txt
$ ls -l archive.tar archive.tar.gz
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 12 20:15 archive.tar
-rw-r--r-- 1 abhisheknair abhisheknair   188 Sep 12 20:21 archive.tar.gz
$

You’ll be able to see that the scale of each archive recordsdata differ considerably although each include the identical three recordsdata. This is because of the usage of compression z flag.

Create compressed archive (BZ2)

tar helps a number of different compression codecs. One in all them is bz2 or bzip2 what’s represented by extension tar.bz2 or generally if tbz2. It’d provide you with a smaller archive measurement, however in flip consumes extra CPU, so the method of compressing/decompressing is perhaps slower than gz archive.

Instance:

$ tar cvjf archive.tar.bz2 file*
file1.txt
file2.txt
file3.txt
$ ls -l archive.tar archive.tar.gz archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 12 20:15 archive.tar
-rw-r--r-- 1 abhisheknair abhisheknair   212 Sep 12 20:25 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair   188 Sep 12 20:21 archive.tar.gz
$ file archive.tar*
archive.tar:     POSIX tar archive (GNU)
archive.tar.bz2: bzip2 compressed information, block measurement = 900k
archive.tar.gz:  gzip compressed information, from Unix, authentic measurement modulo 2^32 10240
$

Extract all recordsdata

A tar archive (compressed or uncompressed) may be simply extracted utilizing the x selection. The examples under will make clear its use:

$ tar xvf archive.tar
file1.txt
file2.txt
file3.txt
$ ls -l
complete 24
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 19 18:25 archive.tar
-rw-r--r-- 1 abhisheknair abhisheknair    13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair    19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair    24 Sep 12 20:08 file3.txt
$

This works for one gz compressed archive as:

$ tar xvf archive.tar.gz
file1.txt
file2.txt
file3.txt
$ ls -l
complete 16
-rw-r--r-- 1 abhisheknair abhisheknair 188 Sep 19 18:27 archive.tar.gz
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair  19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair  24 Sep 12 20:08 file3.txt
$

And even for one bz2 compressed archive as:

$ tar xvf archive.tar.bz2
file1.txt
file2.txt
file3.txt
$ ls -l
complete 16
-rw-r--r-- 1 abhisheknair abhisheknair 212 Sep 19 18:31 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair  19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair  24 Sep 12 20:08 file3.txt
$

Listing tar content material

To listing the contents of a tar archive, you should use t flag as proven under:

$ tar tvf archive.tar.bz2
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
$

Extract particular recordsdata

Just one file may be extracted from a tar or tar.gz or tar.bz2 archive by specifying the file identify as:

$ tar xvf archive.tar.bz2 file1.txt
file1.txt
$ ls -l
complete 8
-rw-r--r-- 1 abhisheknair abhisheknair 212 Sep 19 18:31 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
$

Equally, you may specify a number of file names separated by an area to extract them collectively directly.

$ tar xvf archive.tar.bz2 file1.txt file3.txt
file1.txt
file3.txt
$ ls -l
complete 12
-rw-r--r-- 1 abhisheknair abhisheknair 212 Sep 19 18:31 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair  24 Sep 12 20:08 file3.txt
$

Untar utilizing Wildcard

To extract a number of recordsdata utilizing a wildcard character PATTERNutilization --wildcards flag:

$ tar xvf archive.tar.bz2 --wildcards "file*"
file1.txt
file2.txt
file3.txt
$ ls -l
complete 16
-rw-r--r-- 1 abhisheknair abhisheknair 212 Sep 19 18:31 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair  19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair  24 Sep 12 20:08 file3.txt
$

Add recordsdata to the archive

New recordsdata may be added/added to current uncompressed tarballs utilizing r or --append flag with new filenames or a wildcard sample (keep in mind this solely works with uncompressed .tar recordsdata and never with tar.gz or tar.bz2 compressed codecs):

$ tar rvf archive.tar file-new*
file-new.txt
file-new2.txt
$ tar tvf archive.tar
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
-rw-r--r-- abhisheknair/abhisheknair 15 2021-09-19 18:59 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 10 2021-09-19 18:58 file4.txt
-rw-r--r-- abhisheknair/abhisheknair  9 2021-09-19 19:10 file-new.txt
-rw-r--r-- abhisheknair/abhisheknair  9 2021-09-19 19:10 file-new2.txt
$

You’ll be able to view the contents of the listing archive.tar once more reveals the 2 newly added recordsdata.

Take away recordsdata from archive

Deleting particular recordsdata from a tar archive is feasible utilizing --delete flag as proven under (evaluate the tar listing earlier than and after deleting recordsdata):

$ tar tvf archive.tar
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
-rw-r--r-- abhisheknair/abhisheknair 15 2021-09-19 18:59 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 10 2021-09-19 18:58 file4.txt
-rw-r--r-- abhisheknair/abhisheknair  9 2021-09-19 19:10 file-new.txt
-rw-r--r-- abhisheknair/abhisheknair  9 2021-09-19 19:10 file-new2.txt
$ tar --delete -f archive.tar file-new.txt file-new2.txt
$ tar tvf archive.tar
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
-rw-r--r-- abhisheknair/abhisheknair 15 2021-09-19 18:59 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 10 2021-09-19 18:58 file4.txt
$

Once more this solely works for uncompressed tarballs and can fail for compressed archive codecs.

Create with Confirm

Whereas creating uncompressed tar recordsdata, you may confirm the contents of the archive utilizing W flag as:

$ tar cvfW archive.tar file*.txt
file1.txt
file2.txt
file3.txt
Confirm file1.txt
Confirm file2.txt
Confirm file3.txt
$

This can’t be used with compression flags, though you may compress the created file tar use file later gzip or different instruments.

Extract tar to folder

If you wish to extract your tarball contents to a particular listing as a substitute of the present listing, use -C flag with the folder path as proven under:

$ tar xvf archive.tar -C new-directory/
file1.txt
file2.txt
file3.txt
file2.txt
file4.txt
$ ls -l new-directory/
complete 16
-rw-r--r-- 1 abhisheknair abhisheknair 13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair 15 Sep 19 18:59 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair 24 Sep 12 20:08 file3.txt
-rw-r--r-- 1 abhisheknair abhisheknair 10 Sep 19 18:58 file4.txt
$

Use diff flag

You should utilize --diff or d flag to seek out any adjustments between the recordsdata within the tar archive and people within the file system. Here is an instance the place the diff as soon as when the file contained in the tar and outdoors was the identical. After the file was up to date, it was run once more to indicate the distinction in output.

$ tar dvf archive.tar file4.txt
file4.txt
$
$ echo newline > file4.txt
$
$ tar dvf archive.tar file4.txt
file4.txt
file4.txt: Mod time differs
file4.txt: Dimension differs
$

Exclude recordsdata

Excluding particular recordsdata could also be a requirement when creating tar archives. This may be achieved with --exclude flag.

$ tar --exclude="dir/file2.txt" --exclude="dir/file-new*.txt" -cvzf archive.tar.gz dir/
dir/
dir/file1.txt
dir/file3.txt
$ ls -l dir
complete 24
-rw-r--r-- 1 abhisheknair abhisheknair 9 Sep 19 19:10 file-new.txt
-rw-r--r-- 1 abhisheknair abhisheknair 9 Sep 19 19:10 file-new2.txt
-rw-r--r-- 1 abhisheknair abhisheknair 5 Sep 19 19:20 file-new3.txt
-rw-r--r-- 1 abhisheknair abhisheknair 5 Sep 19 19:27 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair 6 Sep 19 19:27 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair 8 Sep 19 19:27 file3.txt
$ tar tvf archive.tar.gz
drwxr-xr-x abhisheknair/abhisheknair 0 2021-09-19 19:30 dir/
-rw-r--r-- abhisheknair/abhisheknair 5 2021-09-19 19:27 dir/file1.txt
-rw-r--r-- abhisheknair/abhisheknair 8 2021-09-19 19:27 dir/file3.txt
$

As you may inform from the output above, we will use the --exclude mark a number of instances to specify a number of file names or patterns AND situation. Observe that of the six recordsdata within the dir within the instance above, solely two recordsdata met the situation to be included archive.at.gz.

View the tar content material measurement

We are able to get the scale of the contents of a compressed tar archive utilizing the command under:

$ tar tvf archive.tar.gz
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
$ tar -xzf archive.tar.gz --to-stdout|wc -c
56
$

Likewise for bz2 archive:

$ tar tvf archive.tar.bz2
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
$ tar -xjf archive.tar.bz2 --to-stdout|wc -c
56
$

Protect permissions

default tar command retains permission for recordsdata and directories being archived, though you may specify this explicitly utilizing -p flag or --preserve-permissions as proven under:

$ tar cvpzf archive.tar.gz *.txt
file1.txt
file2.txt
file3.txt
$

Abstract 👨‍💻

tar has lengthy been a helpful utility on Unix/Linux methods, primarily used for archiving and backup duties. The utility has developed over time with many choices. It may be used for easy to complicated duties, supplied the options it gives. This text discusses some fundamental operations that you may carry out tar command and reveals the way it may also help you along with your day-to-day system administration duties.

Try its man web page man tar or use tar --help or tar --usage order for extra particulars.

Rate this post
Leave a Comment