Posted in

How to compress a file in Terminal?

If you’ve ever spent hours debugging a server issue only to realize a large log file is slowing your SSH transfer, or tried to free up disk space on a remote machine without a GUI, you know how frustrating uncompressed files can be. That’s where terminal file compression comes in—and as a terminal solutions provider, I’ve seen first-hand how this simple skill cuts down on bandwidth costs, speeds up backups, and keeps remote workflows running smoothly. Let’s break down how to compress files in terminal, tailored for anyone who lives in the command line, whether you’re a seasoned sysadmin or just getting started. Terminal

First, let’s talk about why terminal compression beats GUI tools for most tech teams. When you use a file manager’s "compress" option, it often relies on default settings that work for casual use but not for production environments. For example, a GUI might use a slow, high-compression algorithm that’s unnecessary if you’re transferring files over a fast local network, or skip error correction that saves you from corrupted backups. Terminal tools let you tweak every setting to match your workflow—and as a company that builds terminal software for infrastructure teams, we’ve designed tools that make this customization accessible, even to those who don’t memorize every flag.

The most common compression tools in terminal are gzip, tar, zip, and 7zip, each with its own use case. Let’s start with gzip, since it’s pre-installed on nearly every Unix-based system (Linux, macOS, BSD) and is great for single files. To compress a file with gzip, all you need to do is open your terminal, navigate to the directory with the file using the cd command, and run gzip filename.txt. That command will replace filename.txt with filename.txt.gz, reducing its size drastically. If you want to keep the original file while creating a compressed version, add the -k flag: gzip -k filename.txt. This is perfect if you’re testing compression settings and don’t want to lose the source file. One thing to note: gzip only works on individual files, so if you want to compress a folder, you’ll need to pair it with tar, which stands for "tape archive"—originally made for tape backups, now the standard for bundling multiple files into one.

For folders, the go-to combo is tar and gzip, often called a "tar.gz" or "tgz" file. To compress a folder named project-docs into a single tar.gz file, run tar -czf project-docs.tar.gz project-docs. Let’s break down the flags here: -c means create a new archive, -z tells tar to compress the archive with gzip, and -f specifies the output file name. That’s it—one command to bundle and compress an entire folder. If you want more control, you can adjust the compression level: gzip has levels from 1 (fastest, least compression) to 9 (slowest, most compression). For example, tar -czf -9 project-docs.tar.gz project-docs will use the highest compression, which is useful for long-term backups where you don’t mind waiting a few extra minutes to save space. If you’re working with large files, the -v flag adds verbose output so you can watch the compression progress, which is helpful to confirm the command is running without errors.

Another tool you’ll encounter is zip, which is cross-platform and works on both Unix and Windows systems. That makes it ideal if you need to share a compressed folder with someone who’s using a GUI on Windows, since zip files are universally supported. To compress a folder with zip, run zip -r project-docs.zip project-docs. The -r flag stands for recursive, meaning it will include all subfolders and files inside project-docs. Similar to gzip, you can adjust compression levels with flags like -1 (fast) to -9 (max), or even skip compression entirely with -0 if you’re archiving already compressed files like JPEGs or MP4s, which won’t shrink much with further compression. One downside of zip compared to tar.gz is that it’s slightly less efficient in terms of file size, so if you’re only working with Unix systems, tar.gz is usually the better choice for smaller archives.

For even higher compression, there’s 7zip, a tool that uses the LZMA algorithm which often compresses files 10-15% smaller than gzip or zip. While 7zip isn’t pre-installed on most systems, it’s easy to add—on Ubuntu, you can install it with sudo apt install p7zip-full, and on macOS with brew install p7zip. To compress a folder with 7zip, run 7z a project-docs.7z project-docs. The a flag stands for "add" to the archive, and the .7z extension is the 7zip format. 7zip also supports encryption, which is a big plus for sensitive files—add the -mhe=on flag to encrypt the archive header, so someone can’t see the file names inside the archive without the password. This is a critical feature for compressing client data or internal project files that you’re transferring over unsecure networks, and something our terminal software integrates natively to keep your data safe.

Now, let’s cover some common mistakes and best practices that I see teams make all the time. First, never compress a compressed file. If you take an already compressed ZIP file and run gzip on it, you’ll end up with a file that’s actually larger, because the compression tool can’t find enough redundant data to shrink it further. Save yourself the time and storage—only compress raw files like text, images, or unstructured data. Second, always test your compressed files after creation. A corrupted tar.gz file can ruin a backup, so after running your compression command, use tar -tf project-docs.tar.gz to list the contents and confirm all files are there, or extract a sample file with tar -xf project-docs.tar.gz filename.txt. Our terminal solutions include a built-in integrity check that runs automatically after compression, so you never have to worry about corrupted archives again.

Another tip: if you’re compressing files for transfer, use a tool like pigz instead of gzip. Pigz stands for "parallel gzip" and splits the compression task across multiple CPU cores, which can speed up compression by 2-4x on modern machines. This is a game-changer for large files—for example, a 10GB log file that would take 5 minutes with gzip can take 1.5 minutes with pigz. To use pigz, just install it (sudo apt install pigz) and replace gzip with pigz in your commands, or use tar -czf project-docs.tar.gz –use-compress-program=pigz project-docs. If you’re managing multiple servers, this parallel compression is essential for keeping your backup windows short.

Let’s also talk about compression for remote workflows, since that’s where terminal tools shine. If you’re connecting to a remote server via SSH, transferring uncompressed files will eat up bandwidth and time, but compressing them before transfer cuts down on transfer size. For example, if you need to download a large log file from a remote server, instead of scp user@server:/var/logs/app.log . (which transfers the uncompressed file), you can run ssh user@server "gzip -c /var/logs/app.log" > app.log.gz. The -c flag in gzip tells it to write the compressed output to standard output, so you can pipe it directly to your local machine, avoiding saving an uncompressed file on the remote server. This is a trick I learned early on when working with low-bandwidth connections, and it’s saved countless hours of waiting for transfers.

Now, why does this matter for your team? As a terminal solutions provider, we know that every second your team spends navigating clunky compression tools is time taken away from building products, debugging issues, or focusing on what matters. Our terminal software simplifies all these compression commands into intuitive one-click actions, with built-in recommendations for compression levels based on your use case (backups, transfers, archiving). We also include real-time monitoring for compression jobs, so you can track progress on large files without running separate commands, and automatic integrity checks to ensure your archives are never corrupted.

If you’re tired of juggling multiple GUI tools just to compress files, or you’re looking to speed up your server backup workflows, it’s time to bring terminal compression into your standard toolkit. The benefits go beyond just saving space—faster transfers, smaller cloud storage bills, and fewer errors from corrupted archives add up to big savings for your team. If you’re ready to optimize your file compression process with terminal tools, don’t hesitate to reach out to our team to discuss how we can tailor our solutions to your specific needs. We’ll walk you through the best tools for your workflow, help you set up parallel compression, and ensure your archives are secure and reliable for every use case.

References

Switch Terminals The Linux Command Line: A Complete Introduction by William E. Shotts Jr.
GNU Core Utilities Manual: Gzip, Tar, Zip, 7zip Documentation
Unix Stack Exchange: Terminal Compression Best Practices for System Administrators
Parallel Compression with Pigz: Practical Guide for Sysadmins
OpenSSH Documentation: Secure Remote File Transfer Best Practices


Hangzhou Lingcheng Technology Co., Ltd.
We are one of the most professional terminal manufacturers and suppliers in China. With a professional production team, we are able to meet the needs of the majority of our customers. Please feel free to wholesale bulk high quality terminal from our factory. Customized orders are welcome.
Address: Room 1613, 16th Floor, Building 3, No. 99 Wangzhou Road, Liangzhu Street, Yuhang District, Hangzhou City, Zhejiang Province
E-mail: LCKJ0408@163.com
WebSite: https://www.lingchengelectric.com/