I came across this simple script on ubuntuforums which I thought was really very useful and worth sharing it on my blog. You can either make a function out of it and put it in .bashrc file or make an executable script and put it in /usr/bin/.
Method 1:
Open your ~/.bashrc file using any editor.
gedit ~/.bashrcCopy and paste the following code at the end of it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 extract-file () { 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 }Now you can use the following command to extract any archive:
extract-file
The command extract-file would be available only to terminals which have been opened after saving the .bashrc file with the above code. Also this code is user-specific, so if another user logs in he cannot use this command.
Method 2:
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" fiThis 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 <filename>
Note that we have used /usr/bin directory here. You can use any other directory included in the PATH environment variable directory or add a new directory in PATH environment variable and use it here.