For effective work on the site, cookies and the processing of personal data are used. By using this site, you agree to the terms of use of the site. More

Quote of day

Maltsev Artem's Personal Site

It isn’t what you have or who you are or where you are or what you are doing that makes you happy or unhappy. It is what you think about it.

Dale Carnegie

tar archive

Sept. 16, 2017, 8:19 p.m.

tar archive creation

tar -cvf file.tar /path/to/file_or_folder/   # create .tar

With .tar.gz and .tar.bz2 formats you can use the compression:

tar -czvf file.tar.gz /path/to/file_or_folder/   # create .tar.gz (popular)
tar -cjvf file.tar.bz2 /path/to/file_or_folder/   # create .tar.bz2

Extract .tar archive

tar -xvf file.tar.gz

Keys of tar command

-c - to create archive
-v - listing of handled files
-f - working with file
-z - archive compression with gzip
-j - archive compression with bzip2
-x - file extract from archive
-C - to go to folder (look more detail below)

More details with compression

Do you note that when you extract archive, tar command create full path to the archived structure of files and folders? Say, we archive a folder called test_folder (containing some files and folders):

$ tar -czvf /home/vivazzi/test_1.tar.gz /home/vivazzi/test_folder/

Next, we create unarchivefolder for our extract results and extract test_1.tar.gz:

$ mkdir /home/vivazzi/unarchive/
$ tar -xvf /home/vivazzi/test_1.tar.gz -C /home/vivazzi/unarchive/

As a result, tar command create full structure of archived files and folders with root folders:

├─ unarchive
│  └─ home
│     └─ vivazzi
│        └─ test_folder
│           ├─ file_1.txt
│           └─ file_2.txt
└─ test_1.tar.gz

But it is not convenient! To get only relative folder structure, you need extract with -C key. In doing so, syntax of command changed:

tar -czvf /home/vivazzi/test_2.tar.gz -C /home/vivazzi/ test_folder

Note that test_folder folder is a separate command parameter, i. e. it is separated by a space. -C key will move to specific folder and create folder structure already relative to value of this key.

And now, let's try to extract:

tar -xvf /home/vivazzi/test_2.tar.gz -C /home/vivazzi/unarchive/

We can see, that command works well.

├─ unarchive
│  └─ test_folder
│     ├─ file_1.txt
│     └─ file_2.txt
└─ test_2.tar.gz

It's extracted without full file path.

Rate this article

5 from 5 (total 3 ratings)

You can send feedback, suggestions or comments on this article using this form:

Fields marked by star ( * ) is required.

Thank you for yor feedback!

Article Author

The right to use this article on this page

Permission is granted to copy an article with its author and reference to the original without using the parameter rel="nofollow" in tag <a>. Usage:

Article Author: Maltsev Artem
Link to article: <a href=""></a>

More: Terms of site usage

Do you need to develop yourself or do you want to earn money?

Are you looking for good IT services or books? Save your time and look at my compilation of recommendations, which I use every day.
Go to recommendations

Comments: 0

You can leave a comment as an unregistered user. But by registering, you will be able to receive notifications of replies, and also have access to your personal account to view your comments.

To comment in one's own name you should log in or sign up in the usual way or through social networks:


There is no search on this site, so I offer to use usual search engine, for example, Google, adding "vivazzi" after your request.

Try it