Compress and archive files/directories in Gnu/Linux OS


Use Gzip to compress


The standard zipping/compression program in Gnu/Linux OS is gzip that is Gnu Zip. The extension of Gzip is “.gz” or “.z

To compress a single file only using gzip just:

$ gzip filename


To compress many files do as shown below:

$ gzip testfile1 testfile2 testfile3

To compress all files in a specific directory

$ gzip -r directoryName
fakhri@dev:~/Documents/egdir2$ cd egdir4
fakhri@dev:~/Documents/egdir2/egdir4$ ls
tf1  tf4  tf5
fakhri@dev:~/Documents/egdir2$ gzip -r egdir4
fakhri@dev:~/Documents/egdir2/egdir4$ ls
tf1.gz  tf4.gz  tf5.gz

Basic decompress command in bash:

$ gzip -d filename.gz

As you can see, you have just to add the “-d” option.

Other useful options in gzip are:

-t --test
Test. Check the compressed file integrity.   

-v --verbose
Verbose. Display the name and percentage  reduction  for  each  file

As an example of “-v” option please check the example below:

fakhri@dev:~$ gzip -v *Finance*
25 04 2022 Finance-20220519.bak:	 81.9% -- replaced with 25 04 2022 Finance-20220519.bak.gz
25 04 2022 Finance-20220524.bak:	 81.9% -- replaced with 25 04 2022 Finance-20220524.bak.gz
25 04 2022 Finance-20220526.bak:	 82.0% -- replaced with 25 04 2022 Finance-20220526.bak.gz
25 04 2022 Finance-20220602.bak:	 82.2% -- replaced with 25 04 2022 Finance-20220602.bak.gz
25 04 2022 Finance-20220603.bak:	 82.2% -- replaced with 25 04 2022 Finance-20220603.bak.gz
 

Tarball to create an archive

Unlike in Windows or MacOS you cannot create an archive and zip it with the same program. To create an archive of multiple files or/and directories in Gnu/Linux you need to use tar also called tarball and tape archive. The extension of the tarball program is is “.tar

Thus, to create a compressed archive you need to use both tar and Gzip together. Thus the file will end with “.tar.gz” or ".tgz“.

We will get in detail on how to do so using the command line.

The archive files using tar do as follows:


$ tar -cvf archiveName.tar file1 file2

-c, –create
Create a new archive.

-v, –verbose

verbosely list files processed

-f, –file

All the files will be archived in the tar file named archiveName.tar

Create an archive for all the files and subdirectories in the current folder:

fakhri@dev:~/Documents/Ttardir2$ ls
t23.tar  t2.tar  t4.tar  t5  t5.tar  tardir4  tdir3  tf2  tf3
fakhri@dev:~/Documents/Ttardir2$ tar cvf tardir30june.tar *
t23.tar
t2.tar
t4.tar
t5
t5.tar
tardir4/
tardir4/tf1
tdir3/
tf2
tf3
fakhri@dev:~/Documents/Ttardir2$ ls
t23.tar  t2.tar  t4.tar  t5  t5.tar  tardir30june.tar  tardir4  tdir3  tf2  tf3
fakhri@dev:~/Documents/Ttardir2$ tar tf tardir30june.tar 
t23.tar
t2.tar
t4.tar
t5
t5.tar
tardir4/
tardir4/tf1
tdir3/
tf2
tf3

The following command is used to display the content of the tardir30june.tar archive:

tar tf tardir30june.tar

To create a gzipped and archived file for all the files and directories in the current directory type the following command:

$ tar cvzf archiveName.tar.gz *

The added “z” option uses gzip to zip the archive.

To extract the tarball archive do so:

$ tar xvf archiveName.tar

To extract a compressed archive with gzip:

$ tar xvfz archiveName.tar.gz


Other useful and common options are:
-t, –list
List the contents of an archive

-k, –keep-old-files
Don’t replace existing files when extracting.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top