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 unarchive
folder 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.
Comments: 0