extract any compressed file in Linux

 

Use the following command to create a new file in /usr/bin directory and launch the gedit.

sudo gedit /usr/bin/extract-file

Copy and paste the following code in gedit and save the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjvf $1 ;;
*.tar.gz) tar xzvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjvf $1 ;;
*.tgz) tar xzvf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract-file" ;;
esac
else
echo "'$1' is not a valid file"
fi

This is essentially the same code but instead of using it as a function, we’ll use it as a script.

Now we’ll add the executable bit to the file:

sudo chmod +x /usr/bin/extract-file

You can now use the following command in terminal to extract (almost) any kind of archive.

extract-file