How to Compress Files Recursively in Ubuntu
This article provides a step-by-step method for compressing every file within a directory and its subdirectories on Ubuntu. It focuses on using the terminal to create compressed archives efficiently, saving disk space and simplifying file management for backups or transfers.
Using the Tar Command
The most standard way to compress files recursively in Ubuntu is
using the tar utility combined with gzip. Open your
terminal and navigate to the location where you want to save the
archive, or specify the full path to the source directory.
Execute the following command:
tar -czvf archive_name.tar.gz /path/to/directory
The flags used in this command perform specific functions: * c: Creates a new archive. * z: Compresses the data using gzip. * v: Enables verbose mode to display files being processed. * f: Defines the filename of the resulting archive.
This process captures the main folder and all nested subfolders into a single compressed file.
Using the Zip Command
For better compatibility with other operating systems like Windows,
you can use the zip tool. If it is not installed, run
sudo apt install zip first.
To compress a directory and all its contents recursively, use the
-r flag:
zip -r archive_name.zip /path/to/directory
This command creates a .zip file that preserves the
directory structure while reducing the overall size. Both methods allow
you to package complex folder structures into a single manageable
file.