tar command is used to combine group multiple files and directories into single archive file. It is also refered as Tarball or Tape Archive(tar).
After archiving, you can compress the tar file using different compression methods like gzip, bzip2, xz as you can compress only a single file. After compression original file will be deleted, to keep original file use “-–keep” option.
To preserve the files/directories permissions use “sudo”
Zip also combines multiple files and directories into single file.
In this following post, you will learn what is tar command in linux and how to use it in linux.
1. To create a tar file, use below command.
tar –-create –-file <tar_file_name.tar> <actual files or path to the file> or
tar cvf <tar_file_name.tar> <actual files>
or
tar –cvf <tar_file_name.tar> <actual files>
With above command, you can use option “-p” to preserve file permissions and option “–s” to preserve the order of the files.
2. To list the created archive or tar file, use below command.
tar -–list –-file <archive.tar>
or
tar tf <archive.tar>
or
tar –tf <archive.tar>
3. To append some additional files with existing tar file, use below command.
tar –append –file <archive.tar> <additional files>
or
tar rf <archive.tar> <additional files>
or
tar –rf <archive.tar> <additional files>
With the above command, you can use option “–u” alog with option “-rf” if you want to update new copy of the existing files.
4. To extract the archived files to present directory, use below command.
tar –-extract –-file <archive.tar>
or
tar -xf archive.tar
5. To extract the archived files into another directory, use below command.
tar –extract –file <archive.tar> --directory <path to directory>
or
tar –xf <archive.tar> -C <path to the directory>
Note : While moving make sure the user is having appropriate access to destination directory, else use of “sudo” is recommended.
Usage of tar commands with compression options:
As mentioned, you can compress a single archive file only with following methods : gzip, bzip2 and xz.
6. To compress a file using gzip method, use below command.
Syntax: gzip <option> <filename>
gzip –keep <filename>
or
gzip –k <filename>
-k or –keep – To keep the original file after compression.
7. To list the contents of gzipped/compressed file, use below command.
gzip –l <filename>
or
gzip –list <filename>
8. To uncompress the gzipped file, use below command.
gunzip <filename.gz>
or
gzip –decompress <filename.gz>
9. To compress a file using bzip2 method, use below command.
Syntax : bzip2 <option> <filename>
bzip2 –keep <filename>
or
bzip2 –k <filename>
-k or –keep – To keep the original file after compression
10. To uncompress the bzipped file, use below command.
buzip2 <filename.bz2>
or
bzip2 –decompress <filename.bz2>
11. To compress a file using xz method, use below command.
xz <filename>
12. To uncompress the xz file, use below command.
unxz <filename.xz>
or
xz –decompress <filename.xz>
13. To zip the files/directories, use below command.
zip archive.zip filename.txt
or
zip archive.zip /Documents
or
zip –r archive.zip /Documents
-r – To recursively zip all the files and folders inside Documents folder.
14. To unzip the archived file, use below command.
unzip archive.zip
15. To archive and compress the file at a same time using gzip method, use below command.
tar –create –gzip –file archive.tar.gz filename
or
tar -cvzf archive.tar.gz filename
c – To create a tar archive file
v – To list the files after archiving.
z – To use gzip compression method after tar archive.
f – To denote the file type as file.
16. To archive and compress the file at a same time using bzip2 method, use below command.
tar –create –bzip –file archive.tar.bz2 filename
or
tar -cvjf archive.tar.bz2 filename
How do you feel about this post? Drop your comments below..