List the Contents of a
- Extract Specific Files and Directories From the Compressed File
tar is a command-line utility that allows us to create and extract
tar archives, and it supports the vast majority of compression programs like
compress. The files compressed using
bzip2 end with either
.tbz2. We can use the
tar command to compress and extract files in
We use the
-x option followed by
-f option and then specify the extracted file.
tar -xf compressed.tar.bz2
It extracts the compressed file
compressed.tar.bz2 by detecting the type of compression used. We can use the same command to extract files compressed with other algorithms also.
For most of the Linux users,
tar utility is installed by default at the installation time. For Windows users, we have a tool named
7-zip to extract
If we want some information about the extraction, we can use the
tar -xvf compressed.tar.bz2
It will print the names of all the extracted files in the terminal.
By default, the contents compressed file will be extracted in the current working directory. To extract the files in a particular directory, we can use the
-C option and specify the path where the file needs to be extracted.
tar -xf compressed.tar.bz2 -C /home/Extracted_Files
It will extract the compressed file
compressed.tar.bz2 in the current working directory to the
Extracted_files directory inside
List the Contents of a
To list the contents of a
tar.bz2 file, we use
-t option with the
tar -tf compressed.tar.bz2
Blues/hey.mp3 1.mp3 2.mp3
It shows all the contents of
To get more details about the content like the owner, file size, time-stamp, we use the
-v option with the
tar -tvf compressed.tar.bz2
Extract Specific Files and Directories From the Compressed File
To only extract specific files and directories from the compressed file, we list the names of files and directories to be extracted after compressed file followed by a space.
tar -xf compressed.tar.bz2 1.mp3 2.mp3
It will only extract the files
2.mp3 from the compressed file.
While specifying the files, we must use the exact path for the file to be extracted, as shown by the
tar command, with
tar -xf compressed.tar.bz2 jazz Rock
It will only extract the directories
Rock from the compressed file.
If we specify the file that doesn’t exist, we get an output saying the file is not found.
tar -xf compressed.tar.bz2 rolling.jpeg
Here if the file
rolling.jpeg is not present, we get the following error:
tar: rolling.jpeg: Not found in archive tar: Exiting with failure status due to previous errors