The Wayback Machine - https://web.archive.org/web/20240306101816/https://www.geeksforgeeks.org/how-to-compress-and-extract-files-using-the-tar-command-on-linux/
Open In App
Related Articles

How to Compress and Extract Files Using the tar Command on Linux

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

An archive is a special file that contains any number of files inside. It can be restored via special programs, for example, tar.inside.

  • .tar – archive files are usually not compressed.
  • .tar.gz – archive file compressed with gzip tool
  • .tar.bz2 – archive file compressed with bzip2 tool

Syntax: 

tar options [archive_name.tar] files_to_archive

The tar command does not create a compressed archive, instead, it uses external utilities like gzip and bzip2.

Command functionality:

OptionFull formatDescription
-a–concatenateConcentrate two archives
-c–createCreating a new archive
-d

–diff

–delete

Showing the difference between archives

Delete file from the archive

-r–appendadd files at the end of the existing archive
-t–listShow archive content
-u–updateUpdate an archive
-x–extract Extract files from the archive

Command parameters:

ParameterFull formatDescription
-C dir–directory=DIRchange directory before executing
-f –file=ARCHIVEUse specified archive file 
-j–bzip2compress using bzip2
-p–same-permissions Save file permissions to file 
-v

–verbose

–total

Show process information

Show final result 

-z–gzip compress using gzip

Example of using the tar command: 

1) Compress one file using the tar command:

tar -czvf one-file-compressed.tar.gz hello_world

2) Compress directory using the tar command:

tar -czvf dir-compressed.tar.gz test_directory/

3) Show the archive content:

tar -tf archive.tar.gz

4) Add content to the existing archive:

tar -rvf existing-archive-name.tar file-directory-to-compress/

5) Update content in an archive: 

6) Compress with bzip2:

tar -cjvf one-file-compressed.tar.bz2 hello_world

7) Extract files from a .tar archive:

tar -xf archive.tar.gz

The same with .tar.gz and .tar.bz2


Last Updated : 29 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials