Compressing and Archiving Files
In this section, we'll learn how to compress and archive files in Linux using various tools. Compressing and archiving files is a useful technique when you want to save disk space, send files over the internet, or backup data.
Compression and Archiving Tools
There are several tools available in Linux to compress and archive files. Some of the popular tools are:
- tar: It stands for "tape archive". It is used to create an archive of one or more files or directories. It doesn't compress the files by default, but it can be used in combination with other compression tools like gzip or bzip2.
- gzip: It is used to compress a single file. It replaces the original file with a compressed file with a .gz extension. It is a fast compression tool but provides moderate compression.
- bzip2: It is another compression tool that provides better compression than gzip. It replaces the original file with a compressed file with a .bz2 extension. It is slower than gzip but provides better compression.
- zip: It is a popular compression and archiving tool in Windows, but it is also available in Linux. It is used to create an archive of one or more files or directories. It compresses the files by default.
Using tar Command
To create an archive using tar command, use the following syntax:
tar -cvf archive.tar file1 file2 directory1
Here, c
stands for create, v
stands for verbose (displays the progress), and f
stands for the filename of the archive. You can add multiple files or directories separated by spaces.
To extract the files from the archive, use the following syntax:
tar -xvf archive.tar
Here, x
stands for extract.
Using gzip and bzip2 Commands
To compress a file using gzip, use the following syntax:
gzip file.txt
This will create a compressed file named file.txt.gz
.
To compress a file using bzip2, use the following syntax:
bzip2 file.txt
This will create a compressed file named file.txt.bz2
.
To decompress a file compressed with gzip or bzip2, use the following syntax:
gzip -d file.txt.gz
or
bzip2 -d file.txt.bz2
Using zip Command
To create a zip archive, use the following syntax:
zip archive.zip file1 file2 directory1
Here, archive.zip
is the name of the archive. You can add multiple files or directories separated by spaces.
To extract the files from the zip archive, use the following syntax:
unzip archive.zip
Conclusion
Compressing and archiving files in Linux can be done easily with various tools available. tar, gzip, bzip2, and zip are some of the popular tools used for this purpose. You can use them based on your requirements and preferences.