Compressing Files
Learn how to compress and extract files using tar
Tar is the most used tool to compress file in different formats, Consult man for more informations and options.
Tar :
to compress all the files under /etc and name it etc-archive, use this command:
tar -cvf etc-archive.tar /etc
-c is for compress
-v is for verbose
-f for file.
to extract a compressed file with name project.tar, use this command :
tar -xvf project.tar
-x is for extract.
-v is for verbose.
-f for file.
Here is a summary of the most common options:
-t -> cat the content of the compressed file.
-x -> extract a compressed file.
-C -> specify a path.
-z -> GZIP format.
-j -> BZIP2 compression .
-J -> XZ compression.
Other tools :
gzip /root/etc.tar.gz /etc -> to compress the contents of /etc under /root.
bzip2 /root/home.tar.bz2 /home -> to compress the contents of /home under /root.
gunzip /root/etc.tar.gz -> to decompress contents of /root/etc.tar.gz
bunzip2 /root/home.tar.bz2 -> to decompress contents of /root/home.tar.bz2
use the -k option to keep the original file.
Practice Time :
Using the tar tool and bzip2 format, compress all the files under /tmp and ensure the archived file is under /root/compressedfile. then decompress it under / and make sure to keep the archived file.
Answer :
tar -cjvf /root/compressedfile/tmp.tar.bz2 /tmp
tar -xvf /root/compressedfile/tmp.tar.bz2 -C /
Last updated
Was this helpful?