In this article we are going to shows how to use tar command in Linux. If you want to learn about tar command in Linux with Examples then this post is ideal for you.
In the Linux, the tar command is used to create a tar file (an archive file) by encapsulating a collection of files and directories. The ‘tar‘ word stands for tape archive. It was basically developed to store files on magnetic tape in compressed format.
It is one of the important command which helps you to create compressed or uncompressed Archive files, display a list of the files and directories included in the tar archive, add a files to an existing tar file, and many other operations.
How to Use tar Command in Linux
At present, most of the Linux distribution comes with GNU tar
pre-installed. The Linux, the basic syntax of the tar command is show as below:
tar [OPTIONS] [ARCHIVE_FILE_NAME] [FILE_NAME(s)]
Here,
Options
--create
(-c
) – Creates a new archive(tar file).--extract
(-x
) – Extract the archive.--list
(-t
) – Display the list of the files and directrices included in the tar file.--verbose
(-v
) – Show the verbose information.--file
(-f
) – Creates archive file with the filename.- -u – Add file to an existing archive file
- -z : It is tells the tar command to create tar file using gzip.
Create Tar Archive
To create a tar archive use the -c
option with the -f
option and pass the name of the archive as show below:
tar -cf file.tar filename1 filename2 filename3
The command above, the tar create an archive file named file.tar
that includes the files named filename1
, filename2
, filename3
.
To create a tar file of the given directory, run the command below:
tar -cf backup.tar /var/www
The command above, create an archive named backup.tar of the /var/www directory. You can use the -v
option to see the files that are being processed.
Create Tar Gz Archive
Gzip is a file format( .gz
) and a software utility that is used for file compression and decompression. It is one of the most used and popular compression algorithms that is used to reduce the size of a given file and keep the original file mode, ownership and timestamp.
To create a tar archives with gzip, use the -z
option with the tar command as show below:
tar -czf archive.tar.gz filename1 filename2
Create Tar Bz2 Archive
Bzip2 is also a popular algorithm for compressing tar archives. To create a tar archives with bzip2, use the-j
option with the tar command as show below:
tar -cjf archive.tar.bz2 filename1 filename2
Listing the Content of Tar Archive
You can use the --list
(-t
) option with the tar
command to list the content of a tar archive without extracting it:
tar -tf archive.tar
Output
file1
file2
file3
Extract the Tar Archive
To extracting the archive, use the --extract
(-x
) option with the tar command, as show below:
tar -xf archive.tar
Extract the Tar Archive to Different Directory
By default, the tar command will extract all the archive contents in the current working directory If you want to extract the archive at another location then use the --directory
(-C
) option with the tar command to extract archive content in a specific given directory:
tar -xf archive.tar -C /home/studiesonline/data
The command above will extract the archive.tar contents to the /home/studiesonline/data
directory.
Extract the tar.gz and tar.bz2 Archives
Run the commands below to extract the tar.gz archive :
tar -xf archive.tar.gz
Run the commands below to extract the tar.bz2 archive :
tar -xf archive.tar.bz2
Extract the Specific Files from Tar Archive
To extract one or more files from a tar archive, pass the list of file names to be extracted after the archive name, as show in below command:
tar -xf archive.tar file1 file2
For extracting one or more directories, run the command below:
tar -xf archive.tar dir1 dir2
Extracting Files using Wildcard
You can also extract the files from a tar archive using wildcard pattern. To do that use use the --wildcards
switch with the tar command:
tar -xf archive.tar --wildcards '*.html'
The above command will extract all the files whose names end with.html
(HTML files).
Add Files to Existing Archive
If you want to add files or directories to an existing tar archive, use the --append
(-r
) option with the tar command:
tar -rvf archive.tar newfilename
Remove Files From Existing Tar Archive
If you want to remove files or directories from an existing tar archive, use the
option with the tar command:--delete
tar --delete -f archive.tar filename1
That’s all
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.
Leave a Reply