? Bash function for extracting file archives of various types

There are many applications for extracting different types of archive files.

It will be difficult for you to remember all the options and parameters of each archiving tool.

Today I came across a simple Bash function for extracting file archives of various types on Linux.

This Bash function can extract the most commonly used archive formats such as .tar.bz2, .tar.bz, .bz2, .rar, .zip and .7z.

You don’t need to use an actual application to archive, unpack the archive!

Just add this function to your ~ / bashrc file and call it to extract archive files.

It will automatically find and use the appropriate archiving tool to extract the files.

No need to remember flags and options.

Bash function for extracting file archives of various types

Open your ~ / .bashrc file:

                        $ nano ~/.bashrc

Add the following snippet at the end:

                        # Bash Function To Extract File Archives Of Various Types
extract () {
     if [ -f $1 ] ; then
         case $1 in
             *.tar.bz2)   tar xjf $1     ;;
             *.tar.gz)    tar xzf $1     ;;
             *.bz2)       bunzip2 $1     ;;
             *.rar)       rar x $1       ;;
             *.gz)        gunzip $1      ;;
             *.tar)       tar xf $1      ;;
             *.tbz2)      tar xjf $1     ;;
             *.tgz)       tar xzf $1     ;;
             *.zip)       unzip $1       ;;
             *.Z)         uncompress $1  ;;
             *.7z)        7z x $1    ;;
             *)           echo "'$1' cannot be extracted via extract()" ;;
         echo "'$1' is not a valid file"

Press Ctrl + o and press ENTER to save the file and then press Ctrl + x to exit the file. Run the following command for the changes to take effect:

                        $ source ~/.bashrc

From now on, you can simply call this function to extract archives of various types.

For example, I’m going to extract a .7z archive file using the command:

                        $ extract archive.7z 

Output example:

                        7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_IN,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Core(TM) i3-2350M CPU @ 2.30GHz (206A7),ASM)

Scanning the drive for archives:
1 file, 16013693 bytes (16 MiB)

Extracting archive: archive.7z
Path = archive.7z
Type = 7z
Physical Size = 16013693
Headers Size = 1204
Method = LZMA:23
Solid = +
Blocks = 1

Everything is Ok                                                               

Folders: 21
Files: 37
Size:       16625007
Compressed: 16013693

Likewise, to extract .zip files, the command would be:

                        $ extract archive.zip 

Please note that you must install the appropriate archive manager before using this function. If your system does not have supported archiving tools installed, you will receive an error message similar to the one below:

                        $ extract archive.zip 
bash: /usr/bin/unzip: No such file or directory

This script is mentioned in many places on the Internet.

The author is Igor Pavlov.

Are you using any other cool Bash features?

Please share them via the comments section.

This can be useful for me and all readers.

Sponsored by the article – Logitech MX Keys review

Related Posts