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:

home/vivazzi
├─ 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.

home/vivazzi
├─ 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!

After clicking the "Send" button, your message will be delivered to me on the mail.

Author of Article

The right to use content on this page http://vivazzi.pro/en/it/tar/:

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

Author of Article: Maltsev Artem
Link to article: <a href="http://vivazzi.pro/en/it/tar/">http://vivazzi.pro/en/it/tar/</a>

More: Terms of site usage

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:

Send

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