
{{ $('Map tags to IDs').item.json.title }}
How to Compress and Extract Files with tar and gzip
Compressing and extracting files is an essential task in Linux for saving disk space and managing file transfers. The tar
(tape archive) command is commonly used for combining multiple files into a single file, while gzip
is used for compression. This tutorial will show you how to use these tools effectively.
1. Installing tar and gzip
In most Linux distributions, tar
and gzip
are pre-installed. To check if they are available, you can run:
tar --version
gzip --version
If they are not installed, you can install them using the following commands:
- For Ubuntu/Debian:
sudo apt update sudo apt install tar gzip -y
- For CentOS/RHEL:
sudo yum install tar gzip -y
2. Compressing Files with tar
To create a compressed archive of files using tar
, use the following command:
tar -cvf archive_name.tar /path/to/directory_or_file
This command does the following:
- -c: Creates a new archive.
- -v: Verbosely lists files processed.
- -f: Specifies the filename of the archive.
For example, to compress a directory called myfolder
:
tar -cvf myfolder.tar myfolder
3. Compressing Files with gzip
To compress a file with gzip
, simply run:
gzip filename
This will compress the file and replace it with a .gz
file. For example:
gzip myfile.txt
To keep the original file while compressing, use the -k
option:
gzip -k myfile.txt
4. Creating a Compressed tar.gz Archive
You can combine both tar
and gzip
to create a compressed tarball file. Use this command:
tar -czvf archive_name.tar.gz /path/to/directory_or_file
In this command:
- -z: Compresses the archive using
gzip
.
For example:
tar -czvf myarchive.tar.gz myfolder
5. Extracting tar.gz Files
To extract a .tar.gz
file, you can use:
tar -xzvf archive_name.tar.gz
In this command:
- -x: Extracts files from the archive.
- -z: Decompresses the archive using
gzip
. - -v: Verbosely lists files being extracted.
- -f: Specifies the filename of the archive.
6. Extracting .tar Files
If you have a plain tar file (without gzip compression), you can extract it using:
tar -xvf archive_name.tar
7. Conclusion
By following this tutorial, you now have the knowledge to effectively compress and extract files using tar
and gzip
on Linux. This ability is essential for managing storage efficiently and transferring files securely.