Using tar and gzip
Sometimes, we want to pack a full directory, including files, into a single file for backup purposes or simply to share it more easily. The command that can help aggregate files into one is tar
.
First, we need to install tar
:
[root@rhel8 ~]# yum install tar -y
We can try by creating, as root
, a backup of the /etc
directory branch:
[root@rhel8 ~]# tar -cf etc-backup.tar /etc tar: Removing leading '/' from member names [root@rhel8 ~]# ls -lh etc-backup.tar -rw-r--r--. 1 root root 21M dic 27 16: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@rhel8 ~]# mkdir tmp [root@rhel8 ~]# cd tmp/ [root@rhel8 tmp]# tar -xf ../etc-backup.tar [root@rhel8 tmp]# ls etc
Let's check the new option used:
-x
: for extraction. It unpacks...