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 How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe