Americas

  • United States
sandra_henrystocker
Unix Dweeb

A trick to enable painless file extraction on Linux

How-To
Jun 29, 20204 mins
Linux

Extracting files from archives isn't difficult, but it can be tedious, especially with all the archive naming conventions in use today. But you can make the task a little easier by putting everything you know about the process into a clever little script.

Extracting files from archives in Linux systems is considerably less painful than tooth extraction, but can seem more complicated. In this post, we will take a look at how you can easily extract files from just about any kind of archive you’re likely to run into on a Linux system.

There’s a pile of them—everything from .gz to .tbz2 files with some variations for how those files are named. Sure, you can memorize all of the various commands available for extracting files from archives along with the options they offer, but you can also just deposit that know-how into a script and stop worrying about the details.

Here we show how to assemble a series of extraction commands into a script that calls the proper command to extract the content of file archives depending on the archive file names. The script starts with some commands to verify that a file name has been provided as an argument or to ask that the person running the script provide one:

#!/bin/bash

if [ $# -eq 0 ]; then
    echo -n “filename> “
    read filename
else
    filename=$1
fi

if [ ! -f “$filename” ]; then
    echo “No such file: $filename”
    exit $?
fi

Got that? The script prompts for a file name if no arguments were offered and uses the argument provided if there is one. It then verifies that the file actually exists. If not, the script exits.

The next step is to use a bash case statement to call the appropriate extraction command for the archive depending on its name. For some of these file types (e.g., .bz2), other commands than tar would also work, but we only include one extraction command for each file naming convention. So, here’s the case statement with the various archive file names.

 case $filename in
    *.tar)      tar xf $filename;;
    *.tar.bz2)  tar xjf $filename;;
    *.tbz)      tar xjf $filename;;
    *.tbz2)     tar xjf $filename;;
    *.tgz)      tar xzf $filename;;
    *.tar.gz)   tar xzf $filename;;
    *.gz)       gunzip $filename;;
    *.bz2)      bunzip2 $filename;;
    *.zip)      unzip $filename;;
    *.Z)        uncompress $filename;;
    *)          echo “No extract option for $filename”
esac

If the file provided to the script has a file extension that doesn’t match any of those known to the script, the “No extract option for $filename” message will be issued. If any archive types that you use are missing, just add them along with the required extraction commands.

Add the bash header to the top of the script, make it executable and you should be ready to go.

#!/bin/bash

if [ $# -eq 0 ]; then
    echo -n “filename> “
    read filename
else
    filename=$1
fi

if [ ! -f “$filename” ]; then
    echo “No such file: $filename”
    exit $?
fi

case $filename in
    *.tar)      tar xf $filename;;
    *.tar.bz2)  tar xjf $filename;;
    *.tbz)      tar xjf $filename;;
    *.tbz2)     tar xjf $filename;;
    *.tgz)      tar xzf $filename;;
    *.tar.gz)   tar xzf $filename;;
    *.gz)       gunzip $filename;;
    *.bz2)      bunzip2 $filename;;
    *.zip)      unzip $filename;;
    *.Z)        uncompress $filename;;
    *.rar)      rar x $filename ;;
    *)       

If you want the script to display the contents of the archive as they are being extracted, add the verbose option (-v) to each string of command arguments:

#!/bin/bash

if [ $# -eq 0 ]; then
    echo -n “filename> “
    read filename
else
    filename=$1
fi

if [ ! -f “$filename” ]; then
    echo “No such file: $filename”
    exit $?
fi

case $filename in
    *.tar)      tar xvf $filename;;
    *.tar.bz2)  tar xvjf $filename;;
    *.tbz)      tar xvjf $filename;;
    *.tbz2)     tar xvjf $filename;;
    *.tgz)      tar xvzf $filename;;
    *.tar.gz)   tar xvzf $filename;;
    *.gz)       gunzip -v $filename;;
    *.bz2)      bunzip2 -v $filename;;
    *.zip)      unzip -v $filename;;
    *.Z)        uncompress -v $filename;;
    *)          echo “No extract option for $filename”
esac

Wrap-up

While it’s certainly possible to create an alias for each extraction command you’re likely to use, it’s easier to let a script provide the command for each file type you encounter than having to stop and work out each of the commands and options yourself.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author