Open In App

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

Last Updated : 13 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Tar archives are special files that bundle multiple files and directories into a single file, making it easier to store, share, and manage. While tar archives themselves are not compressed, they can be combined with various compression utilities to reduce their size. The tar command is highly versatile, allowing users to create, modify, and extract archives using a wide range of options and parameters.

Types of Tar Archives

  • .tar: A basic tar archive that contains multiple files and directories without compression.
  • .tar.gz: A tar archive compressed using the gzip tool, offering a good balance between speed and compression ratio.
  • .tar.bz2: A tar archive compressed using the bzip2 tool, known for providing better compression but at a slower speed compared to gzip.

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.

Key Tar Command Options and Their Uses

Below are some of the most commonly used tar command options and their corresponding full formats and descriptions:

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

Common Tar Command parameters

Here are some useful parameters that enhance the functionality of the tar command:

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:

To compress a single file into a .tar.gz archive, use:

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

2. Compress directory using the tar command

To compress an entire directory, the following command is used:

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

3. Show the archive content

To see what's inside an archive without extracting it:

tar -tf archive.tar.gz

4. Add content to the existing archive

If you want to append more files or directories to an existing archive:

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

5. Update content in an archive

To update files in an archive, use the update option '(-u)', which only adds files that are newer than the corresponding ones in the archive.

6. Compress with bzip2

To compress a file with bzip2, resulting in a .tar.bz2 file:

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

7. Extract files from a .tar archive

Extracting files from a tar archive, regardless of the compression type (.tar, .tar.gz, .tar.bz2), can be done with:

tar -xf archive.tar.gz

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

Conclusion

The 'tar' command is a powerful tool for managing files and directories in Linux and Unix environments. Its flexibility, combined with external compression utilities, makes it ideal for a wide range of archiving tasks. Understanding the various options and parameters can significantly simplify your data management workflows.



Next Article

Similar Reads