How to Compress and Extract files in Linux using tar and gzip
In this post, we will learn how to compress and extract files in the Linux system. As a Linux system admin, it is a crucial skill. we can save our disk space consumption by the help of compression technique there are so many compression techniques in Linux like tar,gz gz2, etc all have their special compression ratio.
But here we will use tar and gzip compression technique.
Compress the files
Here,we will user tar command to compress all the file under /etc.
# tar -cvf conf_backup.tar /etc
c :- create
v :- verbose
f :- file
Using above command we have taken backup of all the configuration files which reside in /etc directory.
After completion of this command a file will we available at your present working directory. you can see it using ls command
root@LinuxFork:~# pwd /root root@LinuxFork:~# ls conf_backup.tar root@LinuxFork:~#
Compress the files using gzip utility
we can compress more size using gzip command, here we can see the size of compressed file size is 3.3 MB.
root@LinuxFork:~# ls -lh total 3.3M -rw-r--r-- 1 root root 3.3M Jul 5 10:46 conf_backup.tar root@LinuxFork:~#
But after performing gzip compression size reduced to 652K so if you want to save more size use gzip compression instead of using tar only.
root@LinuxFork:~# ls -lh total 3.3M -rw-r--r-- 1 root root 3.3M Jul 5 10:55 conf_backup.tar root@LinuxFork:~# gzip conf_backup.tar root@LinuxFork:~# ls -lh total 652K -rw-r--r-- 1 root root 652K Jul 5 10:55 conf_backup.tar.gz root@LinuxFork:~#
Compress files in gzip format using tar command
Now, we are going to compress the file in gzip format in one single step using tar command. earlier we have performed compression first then we have performed gzip compression.
# tar cfzv conf_backup.tar.gz /etc
z :- zip
root@LinuxFork:~# ls -lh total 652K -rw-r--r-- 1 root root 652K Jul 5 11:11 conf_backup.tar.gz root@LinuxFork:~#
List the Content of compressed file
we can also list the content of compressed file using -t option in tar command.
# tar -tvf conf_backup.tar.gz
t :- list the content
Extract the compressed file
Now, we are going to extract the compressed file conf_backup.tar.gz using below command .
# tar xfzv conf_backup.tar.gz
x :- Extract
root@LinuxFork:~# ls conf_backup.tar.gz etc
That’s all, In this we have leaned how to compress files in Linux using tar and gzip command line utility.
I am a professional Cloud Engineer and founder of LinuxFork. I am working on Infrastructure Deployment and focusing on the integration of open source technologies with the Cloud. I am an avid Linux lover and open source enthusiast. mostly, I am working with Redhat and Ubuntu Linux in the production environment. apart from this I always believe in knowledge sharing.