All of us might have faced a situation at some point of time or another while using Linux tar.gz
, tar.bz2
, tbz
extensions. So many archive types, and so many commands to remember. Well, not anymore, thanks to the dtrx tool.
What is Dtrx?
Dtrx stands for “Do The Right Extraction“, it is an open-source and very effective command-line application for *nix systems that simplifies your job of archive extraction easier.
The dtrx command is a replacement of “tar -zxvf
” or “tar -xjf
” commands and it provides a single command to extract archives in a number of different formats including tar, zip, rpm, deb, gem, 7z, cpio, rar, and many more. It can also used to decompress files compressed with bzip2, gzip, etc.
By default, dtrx extracts contents to a dedicated directory and also fixes permission issues (like permission denied) faced by the user while extracting content to ensure that the owner can read and write all those files.
Dtrx Features
- Handles Multiple Archive Types: Extracts many formats with a single command, including tar, zip, rar, gz, bz2, xz, rpm, deb, and self-extracting zip files.
- Keeps Everything Organized: Extracts archives into their own dedicated directories.
- Set Permissions: Ensures users can read and write all files after extraction, maintaining the correct permissions.
- Recursive Extraction: Can find and extract archives within archives.
How to Install Dtrx in Linux
The dtrx tool is by default included in repositories of Ubuntu-based distributions, all you’ve to do is simply do an apt to install it on your system.
sudo apt install dtrx
On RHEL-based distributions and some other Linux distributions, dtrx might not be included in the default repositories. Instead, you can download the latest version of the script directly from the command line.
wget https://github.com/dtrx-py/dtrx/releases/download/8.5.3/dtrx-8.5.3.tar.gz tar -xvf dtrx-8.5.3.tar.gz cd dtrx-8.5.3 sudo python3 setup.py install --prefix=/usr/local
How to Use dtrx Command in Linux
The dtrx command simplifies extraction, so you only need to remember one command instead of multiple syntaxes for different archives.
1. Extracting a Single Archive
To extract a single archive file, simply execute the dtrx command followed by the archive name:
dtrx tecmint-22-10-2024.gz
In addition to simplifying the extraction, it has various options like extracting the file to a folder and recursively extracting all other archives inside a given archive.
2. Extracting Multiple Archives
Consider you have a file called “dtrAll.zip“, which consists of dtr1.zip, dtr2.zip, and dtr3.zip, each containing dtr1, dtr2, and dtr3, respectively.
Instead of having to manually extract the dtrAll zip and then extracting each of the dtr1, dtr2, and dtr3 files, you can directly extract them into their respective folders by using dtrx and selecting the option "a"
, which extracts all the zip files recursively.
dtrx dtrAll.zip
After extraction, the contents of the extracted directory can be verified using the ls command.
cd dtrAll ls
Output:
dtr1 dtr1.zip dtr2 dtr2.zip dtr3 dtr3.zip
3. Extracting a Specific Archive
If you want to extract only the main archive and not the nested archives, select the appropriate option (e.g., N
):
dtrx dtrAll.zip
4. Extract Each Layer of Archives
To extract each layer of an archive on a case-by-case basis, for example, if you want to extract the second layer of archives but not the third layer, you can use the "o"
option.
Consider you have a zip file “dtrNewAll.zip“, which contains “dtrAll.zip” and “dtrNew“. If you want to extract the contents of “dtrNewAll” and “dtrAll” as well, but not of dtr1.zip, dtr2.zip, and dtr3.zip, you can use "o"
and "n"
options as shown below.
dtrx dtrNewAll.zip
First, we select the "o"
option, meaning all archives inside dtrNewAll will be extracted. Later, we select the "n"
option for dtrAll.zip, meaning that the archives inside it (dtr1.zip, dtr2.zip, and dtr3.zip) will not be extracted.
5. Extract Meta-data from .deb, .rpm, and .gem Files
To extract metadata instead of the contents from specific package formats, use the -m
option:
dtrx -m openfire_4.9.0_all.deb dtrx -m openfire-4.9.0-1.noarch.rpm dtrx -m openfire-4.9.0.gem
6. Extract Files to a Dedicated Directory
If you want to extract files into a dedicated directory, you can do so with the "-d"
option as shown below:
dtrx -d /home/tecmint tecmint-22-10-2024.gz
7. List Supported Archive Types
If you want to see the list of supported archive types, you can do so using the dtrx --help
command.
dtrx --help
I think you must give it a try to dtrx, because it’s the only powerful command line tool that gives a single command to decompress any format of archive files. That’s it for now, and don’t forget to leave your note in the comment section.
hi everyone, my 7zip file has a password, can i still use dtxr ? is yes then how?
I use a function in my .bashrc which does the extraction job for me:
extract ()
{
if [ -f $1 ]; then
case $1 in
*.tar.bz2)
tar xvjf $1
;;
*.tar.gz)
tar xvzf $1
;;
*.bz2)
bunzip2 $1
;;
*.rar)
unrar x $1
;;
*.gz)
gunzip $1
;;
*.tar)
tar xvf $1
;;
*.tbz2)
tar xvjf $1
;;
*.tgz)
tar xvzf $1
;;
*.zip)
unzip $1
;;
*.Z)
uncompress $1
;;
*.7z)
7z x $1
;;
*.tar.xz)
tar -xJf $1
;;
*)
echo “‘$1’ cannot be extracted via >extract<"
;;
esac;
else
echo "'$1' is not a valid file";
fi
}
The tar command has autodetection of compression. No need for -zxf or -xjf etc, just use -xf.
It can also be used:
tar xf file
and tar will automatically recognize what compression method was used and extract the file.
Every thing is well except .rar extraction is not working for my case …………………….
Cool Sir :) Thank you ……… Beauty of Linux very Useful tool …………….
Dear Ravi,
Thanks. Good tool and easy to use. One qn:- What does the command ‘python setup.py install –prefix=/usr/local’ actually mean?
The command will install all binary files in /usr/local directory.