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 \u2014 .Z, .gz, .tgz etc. And while the commands aren\u2019t very complex, there sure are a lot of them and many options for each.\nSo, 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.\n\nIn 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.\nSo, the case statement might look like this:\n#!\/bin\/bash\n\nif [ -f $1 ] ; then\n case $1 in\n *.tar.bz2) tar xjf $1 ;;\n *.tar.gz) tar xzf $1 ;;\n *.tar.xz) tar zxvf $1 ;;\n *.bz2) bunzip2 $1 ;;\n *.rar) rar x $1 ;;\n *.gz) gunzip $1 ;;\n *.tar) tar xf $1 ;;\n *.tbz2) tar xjf $1 ;;\n *.tgz) tar xzf $1 ;;\n *.xz) xz -d $1 ;;\n *.zip) unzip $1 ;;\n *.Z) uncompress $1;;\n *) echo "contents of '$1' cannot be extracted" ;;\n esac\nelse\n echo "'$1' is not recognized as a compressed file"\nfi\n\nFortunately, there's quite a bit of consistency among the extraction commands' numerous options. For the commands shown, \u201cx\u201d is the \u201cextract\u201d option, \u201cv\u201d, \u201cj\u201d is the tar command\u2019s option for bzip2 files. \u201cz\u201d is for gzip, and \u201cd\u201d is xz\u2019s decompress. Where shown, \u201cf\u201d is the option to specify the file name.\nIf 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.\nIf 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.\nif [ $? != 0 ]; then\n echo "extraction failed"\n exit 1\nfi\n\nAnother 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.\n\nif [ -f $1 ]; then\n echo -n "Do you want to remove the original file ($1) [Yn]?> "\n read ans\n if [ $ans == \u201cY\u201d ]; then\n rm $1\n if [ $? == 0 ]; then\n echo $1 removed\n else\n echo \u201cERROR: $1 not removed\u201d\n fi \n fi\nfi\n\nThis code removes the original file if it still exists, but only if the user agrees by typing \u201cY\u201d.\nUncompressing and extracting the contents of compressed files isn\u2019t 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 \u2014 especially if you have to deal with compressed files of many types.