tar archive

Sept. 17, 2017, 12:19 a.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

4.2 from 5 (total 5 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

Artem Maltsev

Web-developer, having the knowlenge of programming language - Python, framework - Django, content management system - Django CMS, platform of e-commerce site - Django Shop and many other applications, using this technologies.

The right to use content on this page https://vivazzi.pro/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: Artem Maltsev
Link to article: <a href="https://vivazzi.pro/it/tar/">https://vivazzi.pro/it/tar/</a>

More: Terms of site usage

Comments: 0

You can leave a comment as an unregistered user.

But if you sing up, you can:

  • receive notifications
  • view your comments
  • be able to use all the functions of the developed services

To comment in one's own name you should log in or sign up on Vuspace website

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

Select currency for displaying monetary values