Extract All Compressed Files in Linux With A Single Command

Make Extraction Easy With This Script

There are a lot of compression standard and file formats on both Linux and on Windows such as .tar.gz, .tar.xz, .7z, .rar, .tar.bz .tar, .tbz2, .zip, .bz2, and many more. While you certainly can (and should) remember all the commands. But it requires hard work.

Why not create a script instead that makes the extraction process simple for you? In this article, we are going to do exactly that.

Backup your .bashrc

Before we modify anything in our BashRC, let’s back it up first so that we can restore it if something goes wrong. Open your Terminal application, and type the following commands :

cp .bashrc bash.bak

Install all the extraction packages

For this script to work, we need to have a few packages installed on our system. To do that, open a terminal and type the following commands according to your distribution :

For Ubuntu and Ubuntu-based systems :

sudo apt update && sudo apt install unrar p7zip-full p7zip-rar unzip gzip

For Arch Linux and Arch-based distributions :

yay -S 7-zip && sudo pacman -S unrar unzip gzip

For Fedora Workstations :

sudo dnf install p7zip p7zip-plugins unrar unzip gzip

Write A Bash Script to Extract All Packages

In the terminal, open your bashrc file in your preferred text editor such as nano or vim like this :

nano .bashrc


vim .bashrc

Head over to the bottom and then copy and paste the following script :

extract () {
    if [ -f $1 ] ; then
            case $1 in
            *.tar.bz2)    tar xvjf $1    ;;
            *.tar.gz)    tar xvzf $1    ;;
            *.tar.xz)    tar xf $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        ;;
            *)        echo "don't know how to extract '$1'..." ;;
            echo "'$1' is not a valid file!"
Modify Your Vimrc File
Modify Your Vimrc File

Press Ctrl+O to save the file, and press Ctrl+X to exit in nano text editor. Or Press the Escape key and type :wq in the vim text editor to save and exit.

Reload Bash

Now reload your bash so that it recognizes the newly created bashrc file by either restarting the Terminal or typing the following command :

source .bashrc

Create Compressed Sample Files

Create several folders using this command:

mkdir folder1 folder2 folder3

Now compress them using this command :

tar -czvf folders.tar.gz folder1 folder2 folder3

Remove these folders because we are going to demonstrate their extraction and it will create a copy.

rm -r folder1 folder2 folder3

Extracting Compressed Files Using Our Custom Command

Now we know that extracting a tar.gz file requires us to run tar xzvf archivename command, but we can simply write the following commands to extract any compressed file :

extract folders.tar.gz
Extract Using Our Script
Extraction Using Our Script


As we can see, our little script works like a charm. You can extract any compressed file format using this command by typing extract filename.extension.