Making the process easy without having to memorize a suite of syntactical options Credit: Maurits Verbiest The easiest way to extract the content of compressed files (and compressed archives) on Linux is to prepare a script that both recognizes files by type and uses the proper commands for extracting their contents. Almost every compressed file will have an easily recognizable file extension — .Z, .gz, .tgz etc. And while the commands aren’t very complex, there sure are a lot of them and many options for each. So, why not attack the problem with a script that saves your precious brain cells for more challenging work? Let’s look at an example that you might want to consider. In this script, the order in which the file types are listed is important. File extensions like .tar.gz that incorporate simpler file extensions like .gz have to be checked before .gz so that the proper extraction command is used. The case statement, after all, is going to select the first matching criteria that it encounters. So, the case statement might look like this: #!/bin/bash if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xjf $1 ;; *.tar.gz) tar xzf $1 ;; *.tar.xz) tar zxvf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) rar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar xf $1 ;; *.tbz2) tar xjf $1 ;; *.tgz) tar xzf $1 ;; *.xz) xz -d $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1;; *) echo "contents of '$1' cannot be extracted" ;; esac else echo "'$1' is not recognized as a compressed file" fi Fortunately, there’s quite a bit of consistency among the extraction commands’ numerous options. For the commands shown, “x” is the “extract” option, “v”, “j” is the tar command’s option for bzip2 files. “z” is for gzip, and “d” is xz’s decompress. Where shown, “f” is the option to specify the file name. If a compressed file’s naming convention doesn’t match any of those included, we’ll get an error and a chance to update the script as needed. If an extraction fails, anyone running the command used should generate errors. However, you can also make the problem a little more obvious if you specifically say so. Checking the return code after the compression will let anyone using it know whether the decompress operation succeeded or failed. If the return code is not zero, the script exits. if [ $? != 0 ]; then echo "extraction failed" exit 1 fi Another thing you might want to add is the option of removing the original file once the extraction is done. For some decompression commands (such as uncompress), the file is uncompressed in place, so the original will be gone once the uncompress operation is complete. For most of the decompress commands, however, the extraction will not remove the original file. Adding an option to remove it makes it easier for users to remove the original file if they want. This code would follow the failure exit described above. if [ -f $1 ]; then echo -n "Do you want to remove the original file ($1) [Yn]?> " read ans if [ $ans == “Y” ]; then rm $1 if [ $? == 0 ]; then echo $1 removed else echo “ERROR: $1 not removed” fi fi fi This code removes the original file if it still exists, but only if the user agrees by typing “Y”. Uncompressing and extracting the contents of compressed files isn’t all that complicated, but remembering all of the commands and options certainly is. A script like the one described here can save a lot of time — especially if you have to deal with compressed files of many types. 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