Linux: Extract Compressed Files In Terminal

🔺 For cellphones 🔺

💻️ Welcome and thank you for visiting. Having an issue with your Windows 10 or 11 computer? Use the search tool 🔍️ above and you can find the answers to your issue. All searches remain in house. Enjoy your stay and always RTFM.

How to extract or compress files in Linux terminal.

How to extract compressed files zip, 7z, rar and tar

Extraction of compressed files using the terminal is rather easy.

Open a terminal with Ctrl+Alt+T and cd into the directory the compressed file is in

cd /home/user/Directory-name
Output for the ' cd ' command in terminal.

or in your file manager window where the compressed file is, right click and chose ‘Open in Terminal’.

.ZIP

Compress

zip [options] filename.zip file1 file2 folder/
  • filename: The name of the ZIP file you want to create.
  • file1, file2, folder/: The files or folders you want to compress.
  • [options]: Replace'[options] with one or more or none of the options below.

Common Options for the ZIP Command

  • -r Recursively include files in subdirectories.
  • -u Update an existing ZIP file with new files.
  • -d Delete specific files from the ZIP archive.
  • -e Encrypt the ZIP file with a password.
  • -x Exclude specific files from being added.

Extract

In the terminal that’s cd’d into the directory the zip file is located use this command.

unzip filename.zip

Replacing “filename’ with the name of your file.

.7z (7 zip)

To extract a 7zip file in the Linux terminal, first ensure you have the ‘p7zip’ package installed.

In a terminal use the following command to install ‘p7zip’ package

In Debian/Ubuntu

sudo apt install p7zip-full

In Fedora/CentOS/RHEL

sudo yum install p7zip p7zip-plugins

In Arch Linux

sudo pacman -S p7zip

Compress

Open a terminal in the directory the files you want to compress reside.

To compress all the files in the directory, use this command.

$ 7z a filename.7z *

To compress specific files in the directory, use this command.

$ 7z a filename.7z file1.txt file2.txt

Replace “filename’ with the name you want your 7z file to be.

Extract

Once you have the package installed, cd into the directory the compressed file is in and use the following command.

7z x filename.7z

Replacing “filename’ with the name of your file.

.RAR

Compress

To compress an entire directory recursively:

rar a -r filename.rar /path/to/directory

To compress multiple files into a single RAR file:

rar a filename.rar file1.txt file2.txt file3.txt

Replacing “filename’ with the name of your file.

Extract

To extract a rar file in the Linux terminal, first ensure you have the ‘unrar’ package installed.

In a terminal use the following command to install ‘unrar’ package

In Debian/Ubuntu

sudo apt install unrar

In Fedora

sudo dnf install unrar

In RHEL/CentOS

sudo yum install epel-release
sudo yum install unrar

In Arch Linux

sudo pacman -S unrar

In OpenSUSE

sudo zypper install unrar

Once you have the package installed, cd into the directory the compressed file is in and use the following command.

unrar x filename.rar

Replacing “filename’ with the name of your file.

.TAR

The basic syntax for the tartar command is:

tar [options] [archive-file] [file/directory...]

Common Options

Here are some commonly used options with the tar command:

  • -c Create a new archive
  • -x Extract files from an existing archive
  • -t List the contents of an archive
  • -f Specify the name of the archive file
  • -v Verbose output (shows details of the process)
  • -z Compress the archive using gzip
  • -j Compress the archive using bzip2
  • -J Compress the archive using xz

Creating a Compressed Archive

To compress files into a tar.gz archive, use the following command:

tar -czvf filename.tar.gz /path/to/directory_or_file

Flags

  • -c creates a new archive.
  • -z applies gzip compression.
  • -v provides verbose output.
  • -f specifies the name of the archive.

Example Command

To compress a complete directory , you would run:

tar -czvf filename.tar.gz directory-name

Extract

To extract a tar file in the Linux terminal, use the command,

tar -xvzf filename.tar.gz

replacing ‘filename’ with the name of your tar file.

Flags

  • -x: Extract files from the archive.
  • -v: Verbose mode, which lists files being extracted.
  • -z: check if a string is null
  • -f: Specifies the filename of the archive.

Extracting to a Specific Directory

To extract the files to a specific directory, use the -C option followed by the directory path:

tar -xvzf filename.tar -C target_directory

For example, to extract to a directory named Documents, use:

tar -xvzf filename.tar -C ./Documents

Listing Contents Before Extraction

Before extracting, you may want to list the contents of the .tar file to ensure it contains the files you expect. Use:

tar -tvf filename.tar

This command will display the files in the archive without extracting them.