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:
$ 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:
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
$ tar --list -f archive_filename
Sample Output:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
To exclude when extracting:
$ tar -xf archive_file --exclude=FILE
Sample Output:
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:
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