15+ tar command examples in Linux [Cheat Sheet]

Introduction to tar command

tar stands for tape archive. tar command is used to create and extract the archive file in the Linux system. tar is an important tool as it provides archiving functionality in the system. An archive file compresses all the files and collects them together in a single file. It uses less storage in the device.

The basic syntax of the tar command would be:

Advertisement
$ tar [options] [archive_file [file or directory to be archived]

 

Some Important Options used with tar command

-c : This option creates the archive file.
-f : This option is used to specify the archive file.
-x : It extracts the content from the archive file.
-t : It displays the list of files inside the archive file.
-v : This option shows the detailed or verbose information.
-r : It updates the archive file by adding newer files.
-j : It is used to filter the archive through bzip2.
-z : It filters the archive through gzip.

 

Different examples to use the tar command

In this article, you will find different examples of using tar command in Linux to manage the archive file.

 

1. Create an archive using tar command

To create a new archive file, you can use -c or --create option followed by -f and the archive file name. You also have to specify the file or directory names to be archived. tar command does not create an empty archive file.

$ tar -cf archive_filename filename1 filename2 filename3

OR

$ tar --create -f archive_filename filename1 filename2 filename3

Sample Output:

create archive file and add files using tar command

 

2. tar command to list the contents of archive without extracting

You can use -t or --list option to view the content of a tar archive file.

$ tar -tf archive_filename

OR

Advertisement
$ tar --list -f archive_filename

Sample Output:

tar command to view the content of tar archive file

 

3. Search for files inside archive using tar command

You can also search files using -t option by specifying the file names.

$ tar -tf archive_file files_to_search

Sample Output:

tar command to search files in tar archive

 

4. Display the information verbosely using tar command

-v or --verbose option prints the verbose information for every file processed.

$ tar -v [options] archive_filename

OR

$ tar --verbose [options] archive_filename

Sample Output:

tar command to print the information verbosely

 

5. tar command to archive all files in any directory

To add all the contents of the current directory to tar archive, you can use * instead of the file name.

$ tar -cf archive_filename *

Sample Output:

Advertisement

tar command to add all contents to tar archive file

 

6. tar command to archive only specified file type

You can specify a file format that you want to add to the archive file.

$ tar -cf archive_filename *.file_type

Sample Output:

tar command to add only specified file format to archive file

 

7. Extract files from an archive using tar command

To extract files from the tar archive file, you can use -x or --extract  or --get option followed by -f and the archive file name.

$ tar -xf archive_filename 

OR

$ tar --extract -f archive_filename 

OR

$ tar --get -f archive_filename 

Sample Output:

tar command to extract files from tar archive file

 

8. Extract specific files from archive using tar command

You can specify the file name which you want to extract from an archive file. It only extracts those files from the archive file.

$ tar -xf archive_filename filename1 filename2 

Sample Output:

tar command to extract specific file from the tar archive file

 

9. Append files to an existing archive using tar command

You can use -r option add a file or directory to an existing tar archive file. It appends files to the end of an archive.

$ tar -rf archive_filename file_to_be_added

Output:

tar command to append files to the end of an archive

 

10. Merge one archive into another archive using tar command

-A option allows adding contents of one archive to another. It appends files to the end of another archive.

$ tar -Af archive_file archive_files_to_be_added

Sample Output:

tar command to append archive to the end of another archive

 

11. tar command to delete files inside the archive

--delete option allows you to delete the files from archive. You must specify files inside the archive as arguments.

$ tar --delete -f archive_file files_to_be_deleted

Sample Output:

tar command to delete files from the archive

 

12. tar command to find differences between archive and file

You can use -d or --diff or --compare option to find differences between archive and file.

$ tar -df archive_file file_name

OR

$ tar --diff -f archive_file file_name

OR

$ tar --compare -f archive_file file_name

Sample Output:

tar command to find differences between archive and file

 

13. Append only newer files in the archive

-u or --update option appends the files which are newer than the copy in the archive. The newer files don't replace the old copies in the archive. They are appended to the end of an archive.

$ tar -uf archive_file files_to_be_added

OR

$ tar --update -f archive_file files_to_be_added

Sample Output:

tar command to append only new files in archive

 

14. Extract archive into a different directory using tar command

-C or --directory=DIR option changes to the specified directory before permitting any operations.

$ tar -xf archive_file -C directory_path

OR

$ tar -xf archive_file --directory=DIR

Sample Output:

tar command to extract archive in another directory

 

15. Create gzip archive using tar command

Gzip is an algorithm for file compression and decompression. To compress files with gzip algorithm, you can use -z option. The gzip file should end with tar.gz or tgz.

$ tar -czf file.tar.gz files_to_be_archived 

Sample Output:

tar command to create gzip archive

To extract tar.gz file, you can use:

$ tar -xzf file.tar.gz

 

16. Create bzip2 archive using tar command

Another common algorithm for file compressing is bzip2. To compress files with the bzip2 algorithm, you can use -j option. The bzip2 archive name should end with tar.bz2 or tbz.

$ tar -cjf file.tar.bz2 files_to_be_archived

Sample Output:

tar command to create bzip2 archive

To extract tar.gz file, you can use:

$ tar -xjf file.tar.bzip2

 

17. Exclude files while creating an archive

--exclude=FILE option excludes the FILE when adding to tar archive or extracting from a tar archive. It is useful when you have to ignore some files in a large number of files.

To exclude when creating:

$ tar -cf archive_file *.file_type --exclude=FILE 

Sample Output:

tar command to exclude files when creating the archive

To exclude when extracting:

$ tar -xf archive_file --exclude=FILE 

Sample Output:

tar command to exclude file when extracting the archive file

 

18. Show block numbers within the archive

-R or --block-number option prints the block numbers within the archive.

$ tar -Rtf archive_file

Sample Output:

tar command to show block number within the archive

 

Conclusion

Now, you can easily use tar command to create and extract the archive file. You can use this command in any Linux distribution for managing the tar archive. If you have any confusion, you can ask the questions in the comment section.

 

What's Next

What is tar --strip-components & zip --junk-paths with examples in Linux

 

Further Reading

man page for tar command

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment