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. Related content how-to Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe