Redhat Linux 9: Cách dùng tar và gzip



Redhat Linux 9: Cách dùng tar và gzip

Redhat Linux 9:   Cách dùng tar và gzip

Link playlist
Red Hat Enterprise Linux Administration
https://www.youtube.com/playlist?list=PLIpLw6v7Z1qnIUiJipjgYEHwxF16TZbCN
2023 09 23 12 40 49

Using tar and gzip
Sometimes, we want to pack a full directory (including files) into a single file for backup purposes or
to simply share it more easily. The command that can help aggregate files into one is tar.
First, we need to install tar:
[root@rhel-instance ~]# yum install tar -y
We can try by creating (as root) a backup of the /etc directory branch:
[root@rhel-instance ~]# tar -cf etc-backup.tar /etc
tar: Removing leading ‘/’ from member names
[root@rhel-instance ~]# ls -lh etc-backup.tar
-rw-r–r–. 1 root root 20M Feb 17 20:08 etc-backup.tar
Let’s check the options used:
• -c: Short for create. tar can put files together but also unpack them.
• -f: Short for file. We specify that the next parameter will be working with a file.
We can try to unpack it:
[root@rhel-instance ~]# mkdir tmp
[root@rhel-instance ~]# cd tmp/
[root@rhel-instance tmp]# tar -xf ../etc-backup.tar
[root@rhel-instance tmp]# ls
etc
Let’s check the new options used:
• -x: for extraction. It unpacks a tar file.
Observe that we created a directory called tmp to work on and that we pointed to the parent
directory of tmp by using the .. shortcut (which refers to the parent directory of the current
working directory).
Using tar and gzip 91
Let’s use gzip to compress a file. We can copy /etc/services and compress it:
[root@rhel-instance tmp]# cp /etc/services .
[root@rhel-instance tmp]# ls -lh services
-rw-r–r–. 1 root root 677K Jun 23 2020 services
[root@rhel-instance tmp]# gzip services
[root@rhel-instance tmp]# ls -lh services.gz
-rw-r–r–. 1 root root 140K Feb 17 20:16 services.gz
Please note that when using gzip, this will compress the specified file, adding the .gz extension
to it, and the original file will not be kept. Also, be aware that the newly created file is one fifth
of the size of the original file.
To recover it, we can run gunzip:
-rw-r–r–. 1 root root 140K Feb 17 20:16 services.gz
[root@rhel-instance tmp]# gunzip services.gz
[root@rhel-instance tmp]# ls -lh services
-rw-r–r–. 1 root root 677K Feb 17 20:16 services
Now, we can combine the two of them, packing and compressing them:
[root@rhel-instance ~]# tar cf etc-backup.tar /etc/
tar: Removing leading ‘/’ from member names
[root@rhel-instance ~]# ls -lh etc-backup.tar
-rw-r–r–. 1 root root 20M Feb 17 20:08 etc-backup.tar
[root@rhel-instance ~]# gzip etc-backup.tar
[root@rhel-instance ~]# ls etc-backup.tar.gz
etc-backup.tar.gz
[root@rhel-instance ~]# ls -lh etc-backup.tar.gz
-rw-r–r–. 1 root root 4,9M Feb 17 20:20 etc-backup.tar.
gz
This way, we pack and compress in two steps.
The tar command is smart enough to be able to perform packing and compression in a
single step:
[root@rhel-instance ~]# rm -f etc-backup.tar.gz
[root@rhel-instance ~]# tar -czf etc-backup.tar.gz /etc/
tar: Removing leading ‘/’ from member names
[root@rhel-instance ~]# ls -lh etc-backup.tar.gz
-rw-r–r–. 1 root root 4,9M Feb 17 20:22 etc-backup.tar.
gz
Basic Commands and Simple Shell Scripts 92
• -z: This compresses the newly created tar file with gzip. It is also usable for decompression.
We may want to review that same option when decompressing:
[root@rhel-instance ~]# cd tmp/
[root@rhel-instance tmp]# rm -rf etc
[root@rhel-instance tmp]# tar -xzf ../etc-backup.tar.gz
[root@rhel-instance tmp]# ls
etc
As you can see, it’s very easy to pack and compress files using tar and gzip. There are other available
compression methods with higher ratios, such as bzip2 or xz, that you may want to try, too. Now,
let’s move on to combine all the commands that we have learned into a powerful way to automate –
by creating shell scripts.