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 find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe