How to use Tar command in Linux | Compress And Extract

How to use Tar command in linux

Anyone who uses linux has used tar more or less to work with compressed file. In this article, I will introduce some common parameters and examples of how to compress file and folder in linux, how to extract, see compressed file content with popular formats such as .tar, .tar.gz, .tar.bz2

  1. Syntaxes (Options) use tar

Common parameters with the tar command.

  • c – create compressed .tar file.
  • x – extract compressed .tar file.
  • v – show the process of compressing or decompressing data to the screen.
  • f – specify compression to file.
  • t – See data in compressed file.
  • j – create compressed file with bzip2 in file.tar.bz2 format
  • z – create a compressed file with gzip in file.tar.gz format.
  • r – add a file and folder to an existing compressed file.
  • –wildcards- find and output any file in compressed file.
  1. Create compressed file with .tar format

[root@srv1 ~]# tar -cvf thuysys-2016.tar /backup/thuysys/backup/thuysys//backup/thuysys/w3-total-cache//backup/thuysys/w3-total-cache/changelog.txt/backup/thuysys/index.php/backup/thuysys/www.confThe  folder / backup / thuysys  compressed command uses the -cf option to create a compressed file named thuysys-2016.tar and show the compression process out of the screen.

3. Create compressed file with .tar.GZ format

[root@srv1 ~]# tar -cvzf nginx-19.tar.gz /etc/nginx/etc/nginx//etc/nginx/cache/etc/nginx/conf.dWe use the -z option to create the compressed file nginx-19.tar.gz, note that the options must stand in the right position cvzf.

4. Create compressed file with .tar.bz2 format

[root@srv1 ~]# tar -cvjf php-fpm-7.tar.bz2 /etc/php-fpm.d/etc/php-fpm.d//etc/nginx/www.conf

Use the -j option to create the compressed file php-fpm-7.tar.bz2.If they have the same data, .tar.bz2 has the highest compression ratio, then tar.gz and finally tar.

5. Decompress .TAR .TAR.GZ .TAR.BZ2 file

[root@srv1 ~]# tar -xvf thuysys-2016.tar -C /thuysys//thuysys//thuysys/w3-total-cache//thuysys/w3-total-cache/changelog.txt/thuysys/index.php/thuysys/www.confUse -x to extract, for .tar.gz .tar.bz2 formats, we just need to replace thuysys-2016.tar the corresponding compressed file format bar.
Because tar saves the absolute path of the file, when decompressing you add the -C option ( the letter C is in capitals) to decompress with the appropriate path.
6. SEE COMPRESSED FILE CONTENT BY TAR

[root@srv1 ~]# tar -tvf mysql.tar | moreThe command will show the compressed file content, you can check whether the compressed data is lost or not, we use the -t parameter.
When your compressed file has too many files, it will show a series out of screen you will not see anything at all. You add | more to output each line to the screen.
7. Extract an any file in compressed file

tar -xvf /backup/lemp.tar.gz wp-includes/default-filters.php

tar -zxvf /backup/lemp.tar.gz wp-includes/default-filters.php

tar -jxvf /backup/lemp.tar.gz wp-includes/default-filters.php

The above command will extract only the wp-includes/default-filters.php file in lemp.tar/tar.gz /tar.gz.
8. Extract a group of similar files

[root@srv1 ~]# tar -xvf /backup/thuysys-2016.tar –wildcards ‘*default.png’wp-includes/images/crystal/default.pngwp-includes/images/media/default.png

The above command will find and extract all the files named default.png with any path. You can extract all the files with the same extension, for example: ‘* .php’. The tar.gz, tar.bz2 formats do the same.
9. Add file and folder to compressed file

To add a file or folder to an existing compressed file, we use the -r parameter, which only applies to the .tar format

[root@srv1 ~]# tar -rvf config.tar abc.txtabc.txt[root@srv1 ~]

# tar -rvf config.tar apacheapache/apache/index.phpapache/httpd.confThe above are some examples of using tar on linux, hopefully these commands help you work with compressed files more flexibly. You have a good command, then add the comment below for everyone to consult.

We will be happy to hear your thoughts

Leave a reply

Unix
Logo