Archiving and Compressing Files In Linux (Module 5.0)

In this subject matter, we will look at how archiving and compressing files in Linux works with various utilities. More so, the conventional way of writing on this site will not be followed due to the nature of the subject matter for better comprehension.

The cp utility with options like “-r” or “-a” is a very good way of backing up files and directories in the Linux operating system. However, this method does not backup the file permissions, ownership, or access control lists. Hence why archiving utility like tar is a very important utility in Linux. The tar utility will backup files and directories the way they are, including their permissions and ownership.

More so, there are other utilities apart from tar that can be used to archive files and directories in Linux. In this subject matter, we will look at “tar” and these other utilities for file/directory archiving.

There is more to what these utilities can do apart from archiving, for example, the tar utility can also compress a file/directory. Yes, there is a difference between archiving and compressing a file. To archive means to backup/store a file/directory, while to compress means to reduce the size of a file/directory, just like zip in windows operating system

In this tutorial, we will look at how we can archive a file and compress a file.

Understanding How To Use tar In Linux To Archive & Compress Files/Directories

The important tar options/flags to take note of are


c=create

v=verbose (show the activities as it takes place, don’t hide it in the background). This is optional.

f=file

r-recursive(i.e. go through everything in a path)

t=view

x=xtract (extract)

z=zip/compress (in gzip format)

j= zip/compress (in bzip2 format)

NOTE: The first option of what you want to do should come first

How To create an archive Using tar With Examples.

To create an archive with tar, use the command,

tar cvf <archive-name> <source-file/directory>

To create an archive from different set of files, use the command,

tar cvf <archive-name> <source-file-1> <source-file-2> <source-file-n>

For Example

To create an archive of the “/etc” file with tar, use the command,

[root@HQEBPRD ~]# tar cvf test_tar /etc

To verify if the archive has been created, use the command,

[root@HQEBPRD ~]# ls -l test_tar

-rw-r--r--. 1 root root 28313600 Apr 28 01:50 test_tar

To verify the type of file it is, use the command,

[root@HQEBPRD ~]# file test_tar

test_tar: POSIX tar archive (GNU)

To view the content of the file, with the “t” option as mentioned above, use the command,

[root@HQEBPRD ~]# tar tf test_tar

etc/
etc/mtab
etc/fstab
etc/crypttab
etc/resolv.conf
etc/dnf/
etc/dnf/modules.d/
etc/dnf/modules.d/container-tools.module
etc/dnf/modules.d/llvm-toolset.module
etc/dnf/modules.d/perl-DBD-SQLite.module
etc/dnf/modules.d/perl-DBI.module
etc/dnf/modules.d/python36.module
etc/dnf/modules.d/satellite-5-client.module
etc/dnf/modules.d/virt.module
etc/dnf/aliases.d/

............................................

If you try to use the “cat” or “more” utility to view a tar archived file, you will get to see some jargon on your screen.

To extract the archived file, use the command,

[root@HQEBPRD ~]# tar xf test_tar

To verify the file has been extracted, use the command,

[root@HQEBPRD ~]# ls

anaconda-ks.cfg  Documents  etc    initial-setup-ks.cfg  nr    Pictures  Templates  Videos
Desktop          Downloads  file1  Music                 okay  Public    test_tar

you can see the etc directory being extracted.

you can also use the command “ls -l etc” to verify,

[root@HQEBPRD ~]# ls -l etc

total 1348
-rw-r--r--.  1 root root        16 Apr 11 18:01 adjtime
-rw-r--r--.  1 root root      1518 Sep 10  2018 aliases
drwxr-xr-x.  3 root root        65 Apr 11 17:55 alsa
drwxr-xr-x.  2 root root      4096 Apr 11 17:59 alternatives
-rw-r--r--.  1 root root       541 Jun 12  2019 anacrontab
-rw-r--r--.  1 root root        55 May 27  2019 asound.conf
-rw-r--r--.  1 root root         1 Aug 12  2018 at.deny
drwxr-x---.  4 root root       100 Apr 11 17:53 audit

..........

To extract the archived file to a different location, for example, to extract to “/tmp” directory, use the command,

[root@HQEBPRD ~]# tar xf test_tar -C /tmp

please note that option C is in uppercase.

To verify the extracted file, use the command,

[root@HQEBPRD ~]# ls -l /tmp/etc

total 1348
-rw-r--r--.  1 root root        16 Apr 11 18:01 adjtime
-rw-r--r--.  1 root root      1518 Sep 10  2018 aliases
drwxr-xr-x.  3 root root        65 Apr 11 17:55 alsa
drwxr-xr-x.  2 root root      4096 Apr 11 17:59 alternatives
-rw-r--r--.  1 root root       541 Jun 12  2019 anacrontab
-rw-r--r--.  1 root root        55 May 27  2019 asound.conf
-rw-r--r--.  1 root root         1 Aug 12  2018 at.deny

.............

How To Compress a File/Directory Using tar With Examples.

To compress a file/directory, use the command,

tar zcf <compressed-file-name> <source-file/directory>

For Example, to compress the file we just archived above, “test_tar” file, use the command,

[root@HQEBPRD ~]# tar zcf comp-file test_tar

Now, confirm the sizes of both files to really see that the file is compressed,

[root@HQEBPRD ~]# du -sch test_tar comp-file

28M     test_tar
6.9M    comp-file
34M     total
[root@HQEBPRD ~]#

To verify that the file is compressed, use the command,

[root@HQEBPRD ~]# file comp-file

comp-file: gzip compressed data, last modified: Wed Apr 29 01:00:33 2020, from Unix, original size 28323840

You will notice that the file is compressed as gzip.

To extract a compressed file, use the command,

[root@HQEBPRD ~]# tar xvf comp-file

test_tar

Since the file was an archived file before compression, you will need to extract the file again if necessary as we did above with the command,

[root@HQEBPRD ~]# tar xf test_tar

I hope you do understand the examples we have done. What you shouldn’t forget is that archiving is different from compressing/zipping a file. However, compressing/zipping a file, of course, will usually involve archiving. In other words, zipping of a file comes with archiving. For more clarity, let’s compress a file that hasn’t been initially archived.

To compress /tmp with tar, use the command,

[root@RHEL ~]# tar zcf file2 /tmp

tar: Removing leading `/' from member names
tar: /tmp/.X11-unix/X1024: socket ignored
tar: /tmp/.X11-unix/X0: socket ignored
tar: /tmp/.ICE-unix/1789: socket ignored
tar: /tmp/.ICE-unix/2508: socket ignored
tar: /tmp/.esd-0/socket: socket ignored




Now, confirm the sizes of both files,

[root@RHEL ~]# du -sch /tmp file2

30M     /tmp
7.7M    file2

To verify that the file is compressed, use the command,

[root@RHEL ~]# file file2

file1: gzip compressed data, last modified: Wed Apr 29 11:39:23 2020, from Unix, original size 28057600

To extract the file, use the command,

[root@RHEL ~]# tar xf file2

confirm the file is extracted

[root@RHEL ~]# ls

anaconda-ks.cfg  Documents  etc    initial-setup-ks.cfg  nr    Pictures  Templates
Desktop          Downloads  file2  Music                 okay  Public    Videos

you can also use the command,

[root@RHEL ~]# ls -l tmp
total 12
drwxr-xr-x. 139 root root 8192 Apr 29 12:00 etc
drwx------.   3 root root   17 Apr 29 12:05 systemd-private-0135870827e94f6494c4ec0980e067d2-bolt.service-7TqflS
drwx------.   3 root root   17 Apr 29 12:01 systemd-private-0135870827e94f6494c4ec0980e067d2-colord.service-LSPx3v
drwx------.   3 root root   17 Apr 29 12:05 systemd-private-0135870827e94f6494c4ec0980e067d2-fwupd.service-3rliY5
drwx------.   3 root root   17 Apr 29 12:00 systemd-private-0135870827e94f6494c4ec0980e067d2-ModemManager.service-iejPy6
drwx------.   3 root root   17 Apr 29 12:00 systemd-private-0135870827e94f6494c4ec0980e067d2-rtkit-daemon.service-82ntuR
drwx------.   2 root root    6 Apr 11 18:18 tracker-extract-files.0
drwx------.   2 root root    6 Apr 29 12:00 vmware-root_989-4257069338

...................

To extract the file to a different location, for example, to extract to “/media”, use the command,

[root@RHEL ~]# tar xvf file1 -C /media

Now, let’s verify the extracted file in “/media”

[root@RHEL ~]# ls -l /media

total 12
drwxr-xr-x. 139 root root 8192 Apr 29 12:00 tmp

In the examples we have done above, we have been able to compress data in gzip format. That is enough for the newbies in real-life cases. Well, for examination purposes, you should know that the tar utility can compress data in gzip and bzip2 format.

To compress in bzip2 format, replace the option z with j.

For example, to compress /etc in bzip2 format, use the command,

[root@RHEL ~]# tar jcf file3 /etc

tar: Removing leading `/' from member names
[root@RHEL ~]#

To verify the compression type, use the command,

[root@RHEL ~]# file file3

file3: bzip2 compressed data, block size = 900k
[root@RHEL ~]#

To check more usage of the tar utility, refer the tar manual page.

Now, you have learnt that files or directories can be compressed in gzip or bzip2 format.

Going forward, gzip and bzip2 are also Linux utilities themselves, it is possible to compress a file or directory by using the gzip or bzip2 utility without using the tar utility. Let’ look at how it can be done with examples.

How To Compress/Zip Files or Directories In Linux Using gzip Utility

For the purpose of this study, create a new file.

[root@RHEL ~]# vi tekneed-file

The file can contain these contents

Jesus is coming soon, repent from your evil ways
He is the only way, the truth and the life
Without holiness, no man shall see the Lord.

save the file.

Now, we have “tekneed-file”, to compress the file, use the command,

[root@RHEL ~]# gzip tekneed-file

Note that the gzip utility automatically adds “.gz” extension to a file,

Now, list the files to verify.

[root@RHEL ~]# ls

anaconda-ks.cfg  Documents  file2                 Music     Public           tmp
comp-file        Downloads  file3                 okay      tekneed-file.gz  Videos
Desktop          file1      initial-setup-ks.cfg  Pictures  Templates
archiving and compressing files in Linux

To verify the contents without unzipping, use the command,

[root@RHEL ~]# gunzip -c tekneed-file.gz

Jesus is coming soon, repent from your evil ways
He is the only way, the truth and the life
Without holiness, no man shall see the Lord

OR

[root@RHEL ~]# zcat tekneed-file.gz

Jesus is coming soon, repent from your evil ways
He is the only way, the truth and the life
Without holiness, no man shall see the Lord
To Unzip, use the command,
[root@RHEL ~]# gunzip tekneed-file.gz

OR

[root@RHEL ~]# gzip -d tekneed-file.gz

To check more usage of the gzip utility, check the gzip manual page.

How To Compress/Zip Files or Directories In Linux Using bzip2 Utility

To compress the “tekneed-file” with bzip2, use the command,

[root@RHEL ~]# bzip2 tekneed-file

Note that the gzip utility automatically adds “.bz2” extension to a file,

To verify, use the command,

[root@RHEL ~]# ls -l tekneed-file.bz2

-rw-r--r--. 1 root root 124 Apr 29 15:39 tekneed-file.bz2
[root@RHEL ~]# file tekneed-file.bz2

tekneed-file.bz2: bzip2 compressed data, block size = 900k

To verify the contents without unzipping, use the command,

[root@RHEL ~]# bzcat tekneed-file.bz2

Jesus is coming soon, repent from your evil ways
He is the only way, the truth and the life
Without holiness, no man shall see the Lord

To Unzip, use the command,

[root@RHEL ~]# bzip2 -d tekneed-file.bz2

To check more usage of the bzip2 utility, refer to the bzip2 manual page.

How To Compress Files/Directories In Linux Using The Zip Utility

To compress a file or directory using the zip utility, use the command,

zip <compressed-file-name> <source-file/directory>

For example, to compress, “tekneed-file”, use the command,

[root@RHEL ~]# zip file4 tekneed-file

  adding: tekneed-file (deflated 17%)

To unzip, use the command,

[root@RHEL ~]# unzip file4.zip

Archive:  file4.zip
replace tekneed-file? [y]es, [n]o, [A]ll, [N]one, [r]ename: r
new name: new-tekneed-file
  inflating: new-tekneed-file

To check more usage of the zip and unzip utility, refer to their manual pages.

Your feedback is welcomed. If you love others, you will share with others.

Be the first to comment

Leave a Reply

Your email address will not be published.


*