Open In App
Related Articles

tar command in Linux with examples

Improve Article
Improve
Save Article
Save
Like Article
Like

The Linux ‘tar’ stands for tape archive, which is used to create Archive and extract the Archive files. tar command in Linux is one of the important commands which provides archiving functionality in Linux. We can use the Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them. 

Syntax of `tar` command in Linux

tar [options] [archive-file] [file or directory to be archived]

Options

Description

-c

Creates an archive by bundling files and directories together.

-x

Extracts files and directories from an existing archive.

-f

Specifies the filename of the archive to be created or extracted.

-t

Displays or lists the files and directories contained within an archive.

-u

Archives and adds new files or directories to an existing archive.

-v

Displays verbose information, providing detailed output during the archiving or extraction process.

-A

Concatenates multiple archive files into a single archive.

-z

Uses gzip compression when creating a tar file, resulting in a compressed archive with the ‘.tar.gz’ extension.

-j

Uses bzip2 compression when creating a tar file, resulting in a compressed archive with the ‘.tar.bz2’ extension.

-W

Verifies the integrity of an archive file, ensuring its contents are not corrupted.

-r

Updates or adds files or directories to an already existing archive without recreating the entire archive.

What is an Archive file? 

An Archive file is a file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space. 

Examples: 

1. Creating an uncompressed tar Archive using option -cvf

This command creates a tar file called file.tar which is the Archive of all .c files in the current directory. 

tar cvf file.tar *.c
  • ‘-c’: Creates a new archive.
  • ‘-v’: Displays verbose output, showing the progress of the archiving process.
  • ‘-f’: Specifies the filename of the archive

Output : 

os2.c
os3.c
os4.c

2. Extracting files from Archive using option -xvf

This command extracts files from Archives. 

tar xvf file.tar
  • ‘-x’: Extracts files from an archive.
  • ‘-v’: Displays verbose output during the extraction process.
  • ‘-f’: Specifies the filename of the archive.

Output :  

os2.c
os3.c
os4.c

3. gzip compression on the tar Archive, using option -z

This command creates a tar file called file.tar.gz which is the Archive of .c files.  

tar cvzf file.tar.gz *.c
  • ‘-z’: Uses gzip compression.
  • ‘-j’: Uses bzip2 compression.
  • ‘-J’: Uses xz compression.

4. Extracting a gzip tar Archive *.tar.gz using option -xvzf :

This command extracts files from tar archived file.tar.gz files.  

tar xvzf file.tar.gz

5. Creating compressed tar archive file in Linux using option -j

This command compresses and creates archive files less than the size of the gzip. Both compress and decompress take more time than gzip.  

tar cvfj file.tar.tbz example.cpp

Output:  

tar cvfj file.tar.tbz example.cpp
example.cpp

tar tvf file.tar.tbz
-rwxrwxrwx root/root        94 2017-09-17 02:47 example.cpp

6. Untar single tar file or specified directory in Linux:

This command will Untar a file in current directory or in a specified directory using -C option.  

tar xvfj file.tar 
or 
tar xvfj file.tar -C path of file in directory 

7. Untar multiple .tar, .tar.gz, .tar.tbz file in Linux:

This command will extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example, the above command will extract “fileA” “fileB” from the archive files.  

tar xvf file.tar "fileA" "fileB" 
or 
tar zxvf file1.tar.gz "fileA" "fileB"
or 
tar jxvf file2.tar.tbz "fileA" "fileB"

8. Check size of existing tar, tar.gz, tar.tbz file in Linux:

The above command will display the size of archive file in Kilobytes (KB).  

tar czf file.tar | wc -c
or 
tar czf file1.tar.gz | wc -c
or 
tar czf file2.tar.tbz | wc -c

9. Update existing tar file in Linux  

tar rvf file.tar *.c

Output:  

os1.c

10. List the contents and specify the tarfile using option -tf

This command will list the entire list of archived files. We can also list for specific content in a tarfile  

tar tf file.tar

Output:  

example.cpp

11. Applying pipe to through ‘grep command’ to find what we are looking for

This command will list only for the mentioned text or image in grep from archived file.  

tar tvf file.tar | grep "text to find" 
or
tar tvf file.tar | grep "filename.file extension"

12. We can pass a file name as an argument to search a tarfile:

This command views the archived files along with their details.  

tar tvf file.tar filename 

13. Viewing the Archive using option -tvf  

tar tvf file.tar

Output:  

-rwxrwxrwx root/root       191 2017-09-17 02:20 os2.c
-rwxrwxrwx root/root       218 2017-09-17 02:20 os3.c
-rwxrwxrwx root/root       493 2017-09-17 02:20 os4.c

What are wildcards in Linux 

Alternatively referred to as a ‘wild character’ or ‘wildcard character’, a wildcard is a symbol used to replace or represent one or more characters. Wildcards are typically either an asterisk (*), which represents one or more characters or question mark (?), which represents a single character. 

14. To search for an image in .png format:

This will extract only files with the extension .png from the archive file.tar. The -wildcards option tells tar to interpret wildcards in the name of the files to be extracted; the filename (*.png) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell. 

$ tar tvf file.tar --wildcards '*.png' 

Note: In the above commands ” * ” is used in place of file name to take all the files present in that particular directory.  

Conclusion

In this article we have discussed the `tar` command which is used for creating, extracting and managing archive files. We have discussed a number of options and capabilities like compress, decompress and manipulate files/directories within the archives. Overall, we can say that by mastering the ‘tar’ command, Linux users gain a powerful tool for effective file management, storage optimization, and data organization.

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L 
=

 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 Jul, 2023
Like Article
Save Article
Previous
Next
Similar Reads