Commands.page Logo

How to Compress Multiple Specific Files in Ubuntu Linux

This article provides a concise guide on combining and compressing selected individual files into a single archive using the Ubuntu terminal. It covers the essential command-line syntax for the most common compression tools, allowing you to backup or transfer specific data efficiently without archiving entire directories.

Using the Tar Command

The tar utility is the standard archiving tool in Linux. To compress multiple specific files into a .tar.gz archive, use the following syntax:

tar -czvf archive_name.tar.gz file1.txt file2.log file3.conf

Flag Breakdown: * -c: Create a new archive. * -z: Compress the archive using gzip. * -v: Verbose output (shows files being processed). * -f: Specify the filename of the archive.

Replace archive_name.tar.gz with your desired archive name and list each specific file you wish to include separated by spaces.

Using the Zip Command

If you need compatibility with Windows or macOS, the zip utility is often preferred. First, ensure it is installed via sudo apt install zip. Then, use this syntax:

zip archive_name.zip file1.txt file2.log file3.conf

Command Breakdown: * zip: The command to create a zip archive. * archive_name.zip: The name of the resulting compressed file. * file1.txt file2.log...: The list of specific files to include.

Verifying the Archive

After creating the archive, you can verify its contents to ensure all specific files were included correctly.

For tar archives:

tar -tzvf archive_name.tar.gz

For zip archives:

unzip -l archive_name.zip

These commands list the contents without extracting them, confirming your specific files are stored within the single archive.