- tar command in Linux with examples
- How To Untar Files In Linux?
- Untar Command Syntax
- Untar tar File
- Untar tar.gz File
- Untar tar.bz2 File
- Untar To Specified Path or Folder
- List Contents of tar File
- List Contents of tar.gz File
- List Contents of tar.bz2 File
- Untar Single File
- Untar Specific Extensions
- Untar Files with Specified Pattern
- How to Untar Files in Linux
- Tar in Linux
- Extracting tar files
- Final thoughts
- About the author
- Sidratul Muntaha
- Untar Tar.gz – Linux Tar Command – HowTo: Extract Tar File
- Untar tar, tar.gz, tar.bx2 Files
- List the Contents of a tar, tar.gz, tar.bz2 Files
- Extract a Single File from a Tarball
- Extract a Single Directory from a Tarball
tar command in Linux with examples
The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the Archive files. tar command in Linux is one of the important command which provides archiving functionality in Linux. We can use Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them.
tar [options] [archive-file] [file or directory to be archived]
Options:
-c : Creates Archive
-x : Extract the archive
-f : creates archive with given filename
-t : displays or lists files in archived file
-u : archives and adds to an existing archive file
-v : Displays Verbose Information
-A : Concatenates the archive files
-z : zip, tells tar command that creates tar file using gzip
-j : filter archive tar file using tbzip
-W : Verify a archive file
-r : update or add file or directory in already existed .tar file
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 current directory.
2. Extracting files from Archive using option -xvf : This command extracts files from Archives.
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.
4. Extracting a gzip tar Archive *.tar.gz using option -xvzf : This command extracts files from tar archived file.tar.gz files.
5. Creating compressed tar archive file in Linux using option -j : This command compresses and creates archive file less than the size of the gzip. Both compress and decompress takes more time then gzip.
$ tar cvfj file.tar.tbz example.cpp
$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
10. list the contents and specify the tarfile using option -tf : This command will list the entire list of archived file. We can also list for specific content in a tarfile
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.
13. Viewing the Archive using option -tvf
-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 above commands ” * ” is used in place of file name to take all the files present in that particular directory.
?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L
This article is contributed by Akansh Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
How To Untar Files In Linux?
The *.tar is a popular archive format used to compress files in Linux and Unix operating systems. The tar format is used with different compression algorithms like gz, bz2, etc. The most important function of the tar format is the ability to store multiple files and directories as a single file where it can be easily compressed. In the tutorial, we examine how to untar files with different compressions algorithms like gz, bz2 etc.
Untar Command Syntax
The tar command has the following syntax which can be used to untar files and folders in different ways.
tar OPTIONS TAR_FILE PATH
- OPTIONS is used to untar different compressions formats.
- TAR_FILE is the tar file.
- PATH is optional and used in the tar file is extracted differently than the current working path.
Untar tar File
A tar file can be untared or extracted with the following command.
Untar tar.gz File
The tar files can be compressed with the gzip as gz format. In the following example, we extract the tar.gz file.
Untar tar.bz2 File
The bz2 is another popular compression format where tar can be compressed with it. The *.tar.bz2 can be untarred like below.
Untar To Specified Path or Folder
The untar operation extract files to the current working directory by default. But we can also specify another directory to untar a tar archive. The path can be specified with the -C option like below.
$ tar -xvf nmap.tar.bz2 -C /home/ismail/Downloads/
List Contents of tar File
A typical tar file contains lots of files and directories. The contents of the tar file can be listed with the following command.
List Contents of tar.gz File
List Contents of tar.bz2 File
Untar Single File
A single file can be untared from a tar archive. The file which will be extracted is added after the tar archive file.
$ tar -xvf nmap.tar.gz nmap/capsule.c
Untar Specific Extensions
Files with specific extensions can be untar using the tar command. In the following example, we untar files with *.txt extension.
$ tar -xvf nmap.tar.gz --wildcards "*.txt"
Untar Files with Specified Pattern
$ tar -xvf nmap.tar.gz --wildcards "Cache*"
How to Untar Files in Linux
Tar is quite a popular archive format, especially on Linux. In many cases, distros use tar archives to deliver package updates. Tar archives are also common to find when it comes to sharing files online.
Check out how to untar files in Linux.
Tar in Linux
For managing tar archives, all the Linux distros come with the tar tool. It’s this tool that we’ll use for extracting the contents of a tar archive.
Let’s demonstrate everything with an example. First, let’s create a tar archive with several files and directories. Here, I’ve created a directory with the name “Ants” that will be transformed into a tar archive.
Now, let’s make a tar archive out of the directory. Here, tar will use various compression algorithms to do the job. It’s a common practice that the compression algorithm dictates the output file name.
To create a tar archive using gzip compression, use the following command.
To create a tar archive using bzip2 compression, use the following command.
To create a tar archive using XZ compression, use the following command.
Extracting tar files
List tar content
The following tar command will list all the files and directories included in the tar archive.
Let’s have a quick breakdown of the flags we used.
- t: It tells tar to list the contents of the archive.
- v: It tells tar to print its action on console.
- f: It tells tar which file to perform the action on.
Extract entire file
Now, we’re ready to extract the tar archives we’ve got at hand. While you needed to use different commands to create different types of tar archives, we can use only a single tar command to extract all of them.
The following tar command will extract any valid tar archive. If files with similar filenames exist, upon extraction, tar will overwrite the files outside the archive.
Here, we’re facing one new tar flag.
If you don’t want tar to overwrite existing data, add the “-k” flag. It tells tar not to overwrite/replace any existing file or directory.
Extract specific files
There are some situations where you don’t need the entire tar archive extracted only to grab a single file. The tar tool offers such flexibility that you can extract only the select few files you need.
For this task, the tar command structure would look like this. Here, the file name would be the file name of your desired file. It must match with the file name that’s inside the tar archive.
If you want to extract a couple of files in such a manner, use the following command structure.
Extract specific directories
This is yet another awesome feature of the tar archive. Assuming the tar archive at your hand contains a directory or more, you can manually tell tar which directory to extract.
The command structure is similar to the section above.
If you want to extract multiple directories, then run the following command.
Final thoughts
Extracting tar archives is quite a simple task. All you need to know is the right tar command. If you’re interested in performing the actions with GUI and using a file manager, then your file manager should have the ability to extract tar archives by default.
In Linux, there are more tools to extract various formats of compressed archives. Check out how to extract compressed archives in Linux.
About the author
Sidratul Muntaha
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.
Untar Tar.gz – Linux Tar Command – HowTo: Extract Tar File
Most of the Linux files that can be downloaded from the Internet are compressed with a tar , tar.gz and tar.bz2 compression formats and it is important to know how to extract such files.
The following article will help you to extract (unpack) and uncompress (untar) – tar , tar.gz and tar.bz2 files from the Linux command line.
You will learn how to list the contents of a tar archive without unpacking it and how to extract only a single file or a single directory.
File extension | Description |
---|---|
tar | Simple TAR archive without compression |
tar.gz | TAR archive compressed with GZIP |
tar.bz2 | TAR archive compressed with BZIP2 |
Cool Tip: No more wasted time! Download from the web and untar in one step from the Linux command line! Read more →
Untar tar, tar.gz, tar.bx2 Files
Extract and uncompress a tar.gz file:
Extract and uncompress a tar.bz2 file:
Option | Description |
---|---|
-x | Extract files from an archive |
-v | Verbosely list files processed |
-f | Specify an archive or a tarball filename |
-z | Decompress the contents of the compressed archive created by gzip program ( tar.gz ) |
-j | Decompress the contents of the compressed archive created by bzip2 program ( tar.bz2 ) |
List the Contents of a tar, tar.gz, tar.bz2 Files
For example, it goes without saying, that it is inadvisable to untar the whole large archive if you need for example to extract only a dingle file or a directory from it.
And of course this is possible with the Linux tar command, but firstly you need to check what is there inside the tarball without unpacking it.
List the contents of a tar file:
List the contents of a tar.gz file:
List the contents of a tar.bz2 file:
Cool Tip: There is no more need to remember all these -xvf , -xvzf , -xvif keys! This awesome bash function permits to extract any archive type with the single extract command! Read more →
Extract a Single File from a Tarball
Extract a file bar.txt , from an archive:
$ tar -xvf foo.tar bar.txt $ tar -xzvf foo.tar.gz bar.txt $ tar -xjvf foo.tar.bz2 bar.txt
You can also specify a path to the file:
$ tar -xvf foo.tar docs/bar.txt $ tar -xzvf foo.tar.gz docs/bar.txt $ tar -xjvf foo.tar.bz2 docs/bar.txt
Extract a Single Directory from a Tarball
Extract a folder, called docs , from an archive:
$ tar -xvf foo.tar docs $ tar -xzvf foo.tar.gz docs $ tar -xjvf foo.tar.bz2 docs
You can also extract some sub-directory:
$ tar -xvf foo.tar docs/images $ tar -xzvf foo.tar.gz docs/images $ tar -xjvf foo.tar.bz2 docs/images