April 30, 2025 - 18:25
Compression and Extraction in Linux Image
Linux

Compression and Extraction in Linux

Comments

Hello again. This time, I’ll explain how to compress files and folders in Linux (Archive) and how to open compressed files. Actually, I will provide the commonly used commands that I prefer.

You should run these commands through SSH or, if you're using Linux, through Console/Terminal.

The first extension is for Windows users: zip extension.

To compress a file:

GENEL
zip DosyaAdi.zip DosyaAdi

What if we want to compress a folder instead of a file? Then, it's good to add the -r parameter. (This is not necessary if you're using Ubuntu.)

GENEL
zip -r klasor.zip KlasorAdi

What if we want to extract zip files?

GENEL
unzip DosyaAdi.zip

This command will extract the zip compressed file. But what if the zip file is password-protected? It's simple. We’ll add the -P parameter and the password to the command, like this:

GENEL
unzip -P DosyaSifresi DosyaAdi.zip

Can we extract the zip file to a different folder? Yes, we can. The command is very simple:

GENEL
unzip DosyaAdi.zip -d /acacagimizklasor

Most of the time, unzip might not be installed by default.

You may need to install it by running: yum install unzip.

Now, after zip, let's move on to the rar extension. Since these are things you know from Windows, I gave them priority.

To compress a file:

GENEL
rar a -ap DosyaAdi.rar DosyaAdi

To compress a file with a password:

GENEL
rar a -ap -p DosyaAdi.rar DosyaAdi
(NOTE: The command will ask for the password, and you need to enter the same password twice.)

You can also compress without the -ap parameter. However, when you use -ap, it will add a recovery record, making the compression more robust. Can we create a split RAR file like in WinRAR? Of course.

GENEL
rar a -v200M DosyaAdi.rar DosyaAdi

This command will compress the files into 200M parts. Before the file extension, it will add .part000XX. Please note this. You can use any value you like.

To extract a rar file:

GENEL
unrar e DosyaAdi.rar

For a password-protected file:

GENEL
unrar e -p DosyaSifresi DosyaAdi.rar

Now, let me tell you about a nice feature of the unrar command. You can list the contents of a rar file without extracting it:

GENEL
unrar l DosyaAdi.rar

Now it's time for gzip and tar, tar.gz extensions. I’ll go into a bit more detail about tar. I hope you won’t get bored.

With gzip, here is how to compress and extract files:

To compress a file:
GENEL
gzip -9 DosyaAdi
To extract a file:
GENEL
gunzip DosyaAdi.gz

For TAR operations:

There isn’t actually a compression method for tar. tar files are just folders and files bundled together as a single file. If the file ends with .tar, it's just a package. If the file ends with .tar.gz and/or .tar.bz2 (basically, if it has the z letter at the end, it is compressed), then the file is compressed.

To create tar files from folders and files, use the following command:

GENEL
tar -cvf dosyalar.tar dosya1 dosya2 dosya3 ....vs

To create a tar.gz file:

GENEL
tar cvzf DosyaAdi.tar.gz DosyaAdi

To create a tar.bz2 file:

GENEL
tar cvjf DosyaAdi.tar.bz2 DosyaAdi

Now, how do we extract these files? To extract tar.gz files:

GENEL
tar xvzf DosyaAdi.tar.gz

To extract tar.bz2 files:

GENEL
tar xvjf DosyaAdi.tar.bz2

To extract tar files:

GENEL
tar -xvf DosyaAdi.tar

To view the contents of a tar file:

GENEL
tar -tf DosyaAdi.tar

What if we want to extract files to a different location, just like in unzip? Here’s how:

GENEL
tar xvzf DosyaAdi.tar.gz -C /nereyeacilacaksa/

Oops, we’ve come across a .tgz file. What should we do? Don’t worry, it will appear. Here’s the command:

GENEL
tar zxvf DosyaAdi.tgz

Well, this tar command has many parameters. What are they? You can get the full list by running tar --help. Here are some parameters from the examples I gave:

-c Create: Indicates that a tar file will be created. This parameter is required when compressing.

-x Extract: Indicates that a tar file will be extracted. This parameter is used when extracting a file.

-t Table of contents: Indicates that the contents of a tar file will be listed.

-v Verbose: Used to list the names of the files being processed on screen during creation or extraction.

-z Indicates that gzip compression will be used for the tar file.

-f File: Indicates the name of the tar file to be created, extracted, or listed in the table of contents.

ANSWER TO THE QUESTION: What if you want to create a tar.gz file and automatically delete the files added to the archive? You will add the --remove-files parameter. Example:

GENEL
tar cvzf DosyaAdi.tar.gz DosyaAdi --remove-files

I think that’s everything I’ve written. If you have any questions, feel free to leave them in the comments. I’ll try to respond as best as I can.

Related Articles

Comments ()

No comments yet. Be the first to comment!

Leave a Comment