This post covers essential commands for extracting common archive formats like .tar.gz, .tar.bz2, .zip, and .7z on Linux. Learn how to unpack files, review contents before extracting, and even extract to specific directories or standard output. Perfect for anyone needing a quick refresher!

.tar.gz

tar -xzfv ukulele_songs.tar.gz -C ~/Documents/Songs/
  • -x: Extract, retrieve the files from the tar file.
  • -v: Verbose, list the files as they are being extracted.
  • -z: Gzip, use gzip to decompress the tar file.
  • -f: File, the name of the tar file we want tar to work with. This option must be followed by the name of the tar file.

.tar.bz2

tar -xvjf guitar_songs.tar.bz2
  • -j: Bzip2, use bzip2 to decompress the tar file.

review the contents of a tar file before you extract it by using the -t (list) option

tar -tf ukulele_songs.tar.gz | less

zip

install package

# debian
apt install zip unzip

extract

# unzip a file in a different directory
unzip myfiles.zip -d /[Directory-Path]
# avoid see all the information about each containment
unzip -q [FileName].zip
# extract only the files need and exclude the rest
unzip [FileName].zip -x "*Filename1*" "*Filename2*"
# avoid overwriting files that are already extracted, use “-n” flag
unzip -n [FileName].zip

compress

zip –r [FileName].zip file1 file2 folder1

7z

install p7zip-full on your Linux machine.

# debian
sudo apt install p7zip-full
# RHEL
sudo dnf install p7zip

extract 7z compressed file in the current directory

7z x [FILE-NAME.7Z]
  • x command is used to extract the file
  • [FILE-NAME.7Z] provide a path or compressed file name

extract 7z compressed files in a specific directory

7z x [PATH/TO/ARCHIVE.7z] -o[PATH/to/OUTPUT]

If the extract point is not available, then 7zip will create a respective directory at the specified output location.

extract 7z compressed file to STDOUT

read file content without extracting it to a physical location. This can be useful if you just want to read the content of the file and nothing else.

7z x -so [PATH/TO/ARCHIVE.7z]

If you try to run this command on a non-textual compressed file, then it will print gibberish characters, which no one can understand. If you did this by mistake, remember to use Ctrl-Z to stop the reading process.