The only thing that’s really hard about extracting from archives on Unix systems is remembering all of the commands and the required options. When you have ten or more possible archive types and could encounter any of them, you just might run out of space in your frontal cortex and need to restore to searching online for some examples of the correct syntax to use. One way to get past this problem is to stuff all the commands you might ever need into a script and make sure the script is clever enough to use the right command for any archive that you are likely to ask it to handle. In the script presented below, we’re going to do several things: Make sure that the person running the script supplies the archive file’s name; Make sure the specified archive actually exists, and; Select the appropriate extraction command and arguments for extracting the archive’s contents when you run the selected extract command. Make sure a file name is provided If the person running the script doesn’t provide a file name as an argument, we prompt for one. if [ $# != 1 ]; then echo -n "file> " read file else file=$1 fi Check that the specified file exists If the file doesn’t exist (or isn’t a file), issue an error and exit with a non-zero return code. if [ ! -f $file ]; then echo "No such file: $file" exit 1 fi Determine file type by evaluating file extensions Use a case statement to match the pattern of any file name presented. For example, *.tar will match any regular tar file and *.bz2 will match any bzip2 file. case $file in *.bz2) bunzip2 $file;; *.gz) gunzip $file;; … esac The script Here’s the script in one piece for easy copying The case statement is arranged with the list of possible file extensions roughly in alphabetical order. The appropriate extract commands are then run depending on the file extensions of the archive files you use it for. #!/bin/bash if [ $# != 1 ]; then echo -n "file> " read file else file=$1 fi if [ ! -f $file ]; then echo "No such file: $file" exit 1 fi case $file in *.bz2) bunzip2 $file;; *.gz) gunzip $file;; *.rar) rar x $file;; *.tar) tar xf $file;; *.tar.7z) 7z e $file;; *.tar.bz2) tar xjf $file;; *.tar.gz) tar xzf $file;; *.tbz2) tar xjf $file;; *.tgz) tar xzf $file;; *.zip) unzip $file;; *.Z) uncompress $file;; *) echo "$file is of unknown file type";; esac You might consider adding a confirmation of a successful extraction at the end — something like this: if [ $? == 0 ]; then echo $file extraction complete fi Another option is to add the verbose options to the commands where they exist (e.g., tar xvf). I prefer not to do this as the file lists are generally too much to look at. If the extraction fails (e.g., if the archive file is corrupt or misnamed), the extraction command selected will generate errors like “./extractFile: line 22: rar: command not found”, so you don’t need to add a warning to indicate when an extraction fails. This script should be trimmed down if you don’t have all the extraction commands shown or you’ll get some errors if you try to extract from an archive when you don’t have the specified extraction command installed. For example, you might not have 7z on your system. If that’s the case, remove that line from the script or comment it out. You can also add lines to the script if you any archive file types have been omitted. And it’s always a good idea to verify that a script works properly before depending on it when you’re in the middle of some important work with your customers breathing down your neck. 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