{"id":3969,"date":"2026-11-26T16:59:00","date_gmt":"2026-11-26T08:59:00","guid":{"rendered":"http:\/\/www.oiseausurprise.com\/blog\/?p=3969"},"modified":"2026-11-26T16:59:00","modified_gmt":"2026-11-26T08:59:00","slug":"how-to-compress-a-file-in-terminal-459e-490daa","status":"publish","type":"post","link":"http:\/\/www.oiseausurprise.com\/blog\/2026\/11\/26\/how-to-compress-a-file-in-terminal-459e-490daa\/","title":{"rendered":"How to compress a file in Terminal?"},"content":{"rendered":"<p>If you\u2019ve 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\u2019s where terminal file compression comes in\u2014and as a terminal solutions provider, I\u2019ve seen first-hand how this simple skill cuts down on bandwidth costs, speeds up backups, and keeps remote workflows running smoothly. Let\u2019s break down how to compress files in terminal, tailored for anyone who lives in the command line, whether you\u2019re a seasoned sysadmin or just getting started. <a href=\"https:\/\/www.lingchengelectric.com\/terminal\/\">Terminal<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.lingchengelectric.com\/uploads\/48003\/small\/brass-threading-insert-nut26914.jpg\"><\/p>\n<p>First, let\u2019s talk about why terminal compression beats GUI tools for most tech teams. When you use a file manager\u2019s &quot;compress&quot; 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\u2019s unnecessary if you\u2019re 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\u2014and as a company that builds terminal software for infrastructure teams, we\u2019ve designed tools that make this customization accessible, even to those who don\u2019t memorize every flag.<\/p>\n<p>The most common compression tools in terminal are gzip, tar, zip, and 7zip, each with its own use case. Let\u2019s start with gzip, since it\u2019s 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\u2019re testing compression settings and don\u2019t 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\u2019ll need to pair it with tar, which stands for &quot;tape archive&quot;\u2014originally made for tape backups, now the standard for bundling multiple files into one.<\/p>\n<p>For folders, the go-to combo is tar and gzip, often called a &quot;tar.gz&quot; or &quot;tgz&quot; file. To compress a folder named project-docs into a single tar.gz file, run tar -czf project-docs.tar.gz project-docs. Let\u2019s 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\u2019s it\u2014one 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\u2019t mind waiting a few extra minutes to save space. If you\u2019re 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.<\/p>\n<p>Another tool you\u2019ll 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\u2019s 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\u2019re archiving already compressed files like JPEGs or MP4s, which won\u2019t shrink much with further compression. One downside of zip compared to tar.gz is that it\u2019s slightly less efficient in terms of file size, so if you\u2019re only working with Unix systems, tar.gz is usually the better choice for smaller archives.<\/p>\n<p>For even higher compression, there\u2019s 7zip, a tool that uses the LZMA algorithm which often compresses files 10-15% smaller than gzip or zip. While 7zip isn\u2019t pre-installed on most systems, it\u2019s easy to add\u2014on 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 &quot;add&quot; to the archive, and the .7z extension is the 7zip format. 7zip also supports encryption, which is a big plus for sensitive files\u2014add the -mhe=on flag to encrypt the archive header, so someone can\u2019t 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\u2019re transferring over unsecure networks, and something our terminal software integrates natively to keep your data safe.<\/p>\n<p>Now, let\u2019s 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\u2019ll end up with a file that\u2019s actually larger, because the compression tool can\u2019t find enough redundant data to shrink it further. Save yourself the time and storage\u2014only 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.<\/p>\n<p>Another tip: if you\u2019re compressing files for transfer, use a tool like pigz instead of gzip. Pigz stands for &quot;parallel gzip&quot; 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\u2014for 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 &#8211;use-compress-program=pigz project-docs. If you\u2019re managing multiple servers, this parallel compression is essential for keeping your backup windows short.<\/p>\n<p>Let\u2019s also talk about compression for remote workflows, since that\u2019s where terminal tools shine. If you\u2019re 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 &quot;gzip -c \/var\/logs\/app.log&quot; &gt; 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\u2019s saved countless hours of waiting for transfers.<\/p>\n<p>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.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.lingchengelectric.com\/uploads\/48003\/small\/m6-screw-block-terminal-for-meter49c05.jpg\"><\/p>\n<p>If you\u2019re tired of juggling multiple GUI tools just to compress files, or you\u2019re looking to speed up your server backup workflows, it\u2019s time to bring terminal compression into your standard toolkit. The benefits go beyond just saving space\u2014faster transfers, smaller cloud storage bills, and fewer errors from corrupted archives add up to big savings for your team. If you\u2019re ready to optimize your file compression process with terminal tools, don\u2019t hesitate to reach out to our team to discuss how we can tailor our solutions to your specific needs. We\u2019ll 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.<\/p>\n<h2>References<\/h2>\n<p><a href=\"https:\/\/www.lingchengelectric.com\/switch-terminals\/\">Switch Terminals<\/a> The Linux Command Line: A Complete Introduction by William E. Shotts Jr.<br \/>\nGNU Core Utilities Manual: Gzip, Tar, Zip, 7zip Documentation<br \/>\nUnix Stack Exchange: Terminal Compression Best Practices for System Administrators<br \/>\nParallel Compression with Pigz: Practical Guide for Sysadmins<br \/>\nOpenSSH Documentation: Secure Remote File Transfer Best Practices<\/p>\n<hr>\n<p><a href=\"https:\/\/www.lingchengelectric.com\/\">Hangzhou Lingcheng Technology Co., Ltd.<\/a><br \/>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.<br \/>Address: Room 1613, 16th Floor, Building 3, No. 99 Wangzhou Road, Liangzhu Street, Yuhang District, Hangzhou City, Zhejiang Province<br \/>E-mail: LCKJ0408@163.com<br \/>WebSite: <a href=\"https:\/\/www.lingchengelectric.com\/\">https:\/\/www.lingchengelectric.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019ve ever spent hours debugging a server issue only to realize a large log file &hellip; <a title=\"How to compress a file in Terminal?\" class=\"hm-read-more\" href=\"http:\/\/www.oiseausurprise.com\/blog\/2026\/11\/26\/how-to-compress-a-file-in-terminal-459e-490daa\/\"><span class=\"screen-reader-text\">How to compress a file in Terminal?<\/span>Read more<\/a><\/p>\n","protected":false},"author":370,"featured_media":3969,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3932],"class_list":["post-3969","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-terminal-4047-494318"],"_links":{"self":[{"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/posts\/3969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/users\/370"}],"replies":[{"embeddable":true,"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/comments?post=3969"}],"version-history":[{"count":0,"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/posts\/3969\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/posts\/3969"}],"wp:attachment":[{"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/media?parent=3969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/categories?post=3969"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.oiseausurprise.com\/blog\/wp-json\/wp\/v2\/tags?post=3969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}