Are you new to Linux? Want to learn how to compress files from the Linux terminal but don’t know how to do it? We can help! Follow along as we go over 4 ways to compress files in the terminal on Linux!
The number one way to compress files in the terminal on the Linux platform is with Tar. Tar is a built-in utility that a lot of Linux programs rely on, so there is no need to go over how to install it.
The Tar command can create archives in many different formats, such as TGZ, Tar, XZ, and others. However, the most versatile and useful archives you can make with Tar is TarGZ.
To compress files in a TarGZ archive, open up a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. From there, make use of the tar command example below to learn how to compress files and folders.
To compress a file (or multiple files), execute the following command example.
First, move into the folder that has the files you wish to compress. In this example, we will be using the “Documents” directory.
Once the terminal is in the “Documents” directory, you create a new TarGZ archive. The command below will compress absolutely everything in the “Documents” folder.
tar -czvf my-new-archive.tar.gz *
Don’t want to compress everything in a folder? Only want to compress a specific thing? Try this command instead.
tar -czvf my-new-archive.tar.gz /home/username/path/to/file/or/folder/
Suffice it to say; the Tar command is incredibly versatile. We’ve just scratched the surface of what you can do to compress files. However, you can learn more about how to use Tar to create archives, execute the –help command below.
Another way to compress files on Linux via the terminal is with Rar. Rar is a free utility that allows those who install it to interact with the RAR archive format. Rar is a proprietary format, so this tool is incredibly useful if you have RAR archives on your Linux PC or know someone who uses the format.
To compress files in with the Rar utility in Linux, you must first install the program. The reason that this program must be installed is that Unrar supports a proprietary format, and many Linux distributions don’t like that fact.
sudo apt install rar
sudo apt-get install rar
sudo pacman -S rar
sudo dnf install https://forensics.cert.org/fedora/cert/32/x86_64/rar-5.4.0-1.fc32.x86_64.rpm
sudo zypper addrepo https://packman.inode.at/suse/openSUSE_Leap_15.2/ packman-x86_64 sudo zypper install rar
Once the Unrar app is set up on your Linux PC, you can use the unrar command to compress files. Using the command examples below, create a Rar archive. Be sure to customize the commands to suit your needs.
rar a -r my-rar-archive.rar /home/username/path/to/folder/
Or, to compress a single file, execute the following command.
rar a my-rar-archive.rar /home/username/path/to/file.file
Lastly, you can compress all files in a folder.
rar a my-rar-archive.rar *
When the Rar compression command finishes running, a RAR format archive will appear in the directory you ran the compression command.
If you need to compress files on your Linux PC that are also easily accessible on other operating systems (like Mac or Windows), Zip is what you want. It’s a universal compression format supported by all of the major operating systems. Here’s how to compress files with it on Linux.
First, open up a terminal window. Once the terminal window is open, follow the command-line examples below to learn how to compress files with the zip command.
To compress a folder containing files with the Zip tool, specify the location of the folder to the zip command. Keep in mind that you will need to customize the command example below to suit your needs.
zip -r name-of-zip-archive /home/username/location/of/folder/
Want to compress just a few files using the Zip tool, and not an entire directory? Try this command example out instead.
First, move into the directory where the files are you wish to compress.
Then, compress the files using the zip command.
zip name-of-zip-archive filename1 filename2 filename 3
Or, compress every file in the folder using the wildcard (*).
zip name-of-zip-archive *
When the zip command completes, you will have a Zip archive in the directory in which you ran zip from.
Pigz is a compression utility that is unlike any of the other programs on this list. See, other apps like Tar, Rar, and Zip can compress multiple files and folders. Not Pigz can only compress a file at a time, but it is good at it.
The thing about Pigz is that it allows users to quickly, efficiently compress a file to send to a friend or to back up. Pigz is very simple. To get started with it, launch a terminal window. Then, follow the instructions below to install it on your system.
Note: want to learn more about the Pigz compression tool? If so, check out our in-depth guide on how to use the Pigz tool on Linux!
sudo apt install pigz
sudo apt-get install pigz
sudo pacman -S pigz
sudo dnf install pigz
sudo zypper install pigz
Now that Pigz is set up move into the directory with the file you wish to compress with Pigz using the CD command. Remember to change the example command below to suit your needs before running!
Once in the directory, compress the file using the pigz command.
When the command finishes, the compression process is done. You’ll find your file replaced with a compressed GZ archive in the folder you ran the command from.
There are many ways to compress files on Linux in the terminal; however, many beginner Linux users are not aware of these tools. If you’ve just started using Linux and wanted to find out different ways of compressing files, hopefully, this list pointed you in the right direction!
What is your favorite tool to use to compress files on Linux? Tell us in the comment section below!