tar command to create and extract archives

In this article, we would discuss how we can create and extract archives using tar command-line utility. tar basically helps us store multiple files in a single file which is an archive file. Later, we can extract the files from the archive as and when required. There are many operations we can execute on our archive which we will discuss here.

tar stands for tape archiver, we can use it to archive files and even devices.

Now, we will first start with creating archives. There are three options we will utilize.

-c which is required to create an archive,
-v for verbose and,
-f this takes an argument, which is the name of our archive.

To create an archive using tar command-line utility –

tar -cvf archive_name.tar /directory_to_be_archived

For instance,

tar -cvf home.tar /home

This will create a home.tar archive which will contain all the files from /home directory. The home.tar will be created in the current directory.

To extract an archive using tar command-line utility –

tar -xvf archive_name.tar

where,

-x is to extract files from an archive.

rest of the options (i.e. -v and -f) remain the same as above.

For instance,

tar -xvf home.tar

this will extract the contents of file home.tar in current directory.

In case, if we want to extract the contents to a specific directory then,

tar -xvf archive_name.tar -C /path/to/extract/archive

For instance,

tar -xvf home.tar -C /home/$USER/Documents/

We can also list the contents of our archive –

tar -tvf archive_name.tar

where,

-t option is used to list contents of an archive.

In conclusion, we have discussed how to create and extract archives using tar command-line utility.

Additional Info –

If you would like to know about tar version installed then issue the following in terminal –

tar --version

To append a file to the end of archive –

tar --append --file=achive_name.tar /path/to/new/file

There is one more interesting option –update. It mainly appends the newer copies of the file at the end of archive while retaining older copies.

tar --update --file=achive_name.tar /path/to/new/file

Similar Posts