How to Compress Files in Linux
In this tutorial, we will examine the Zip*: French command, a useful utility that allows us to specify lists of files, set a level of data compression and create compressed archives.
While you get used to these commands, it’s good to work with sample test files and directories, and you should take extra care to make sure you follow the instructions carefully.
All commands in this tutorial will work on most Linux machines. We used an Ubuntu 20.04 install, but you can run this guide on a Raspberry pie. All procedures are performed through the terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.
Create a Zip Archive in Linux
To start, let’s create some empty test files and then add them to an archive. We will use the to touch command to create files, the ls command to verify that the files were created, then the Zip*: French command to create a base archive.
1. Create a test directory where we can safely try the command. Change directory to find yourself in the new directory.
mkdir test_directory cd test_directory
2. Create test files to add to our archives. Note the 3 different file suffixes.
$ touch test1.txt test2.h test3.c
3. List files to verify that they were created correctly.
4. By using the Zip*: French command, create ZIP archive newly created files. To create a ZIP archive, we simply use the command Zip*: French followed by the name we give to the archive, then a list of files we want to include in the archive. The following command will create a ZIP archive called test_archive.zip and the archive will include the three test files. Note that we don’t need to add the archive suffix .Zip *: French in the name of the archive when using Zip*: French order.
zip test_archive test1.txt test2.h test3.c
5. Verify that the ZIP archive was created.
Creating a ZIP archive by selecting only specific file types
It is possible to provide the Zip*: French command with arguments that only add specific file types to the archive. This is a powerful feature that is efficiently done on the command line.
1. Delete all previous test archives and check that you still have the three original test files. Note that unlike creating the ZIP archive, you must include the .zip file extension when referencing an existing archive.
rm test_archive.zip ls
2. Create a new ZIP archive containing only the test files ending with .h and .vs . After creating the archive, if you check its contents, it will only contain the files test2.h and test3.c and will not contain test1.txt.
zip test_archive *.h *.c
3. To check the content, use less.
Create a ZIP archive containing a directory
Often we need to create a ZIP archive containing directories containing files, we can do this by adding the recursive argument -r to the Zip*: French order.
1. Delete the archives inside the test_directory but keep the other files. To avoid confusion, it would be good to delete the archives leaving only the original files that we created.
2. Go up one directory and verify that you are not in the directory to be archived. We cannot be inside the directory we want to add to the archive.
3. Create a ZIP archive containing the test_directory directory and its contents. Notice when you create the archive using the -r argument you see detailed output detailing each step of the command as it descends into the directory and checks in the files and folder.
zip -r new_archive test_directory
4. Create another similar ZIP archive using -q (silent) argument to disable verbose output. You should see this time that no steps are reported to the terminal in creating this archive. Then use ls to verify that the new archive was created.
zip -r -q new_archive2 test_directory ls
Change the data compression level for a ZIP archive
By default, the Zip*: French The command uses a compression value of 6 taken from a range of 0 to 9, with 0 being uncompressed and 9 being the maximum compression level allowed. We can simply add a numeric argument to change the level of compression.
1. Add random data to test files. At the moment our test files test1.txt, test2.h and test3.c are empty. They don’t have any bytes of data, but we can add random “junk data” to the files using a quick terminal command that will dump 64MB in each file.
cd test_directory head -c 64M test1.txt head -c 64M test2.h head -c 64M test3.c
2. Return to the parent directory and create a new ZIP archive with the default compression level of 6.
cd zip -r -q archive_default_compression test_directory
3. Create another archive with the same content but increase the compression level to 9. Check the details of the new archive and the previous archive using ls -l , you should see that the size of the new archive is smaller than the previous archive because the data compression has been increased.
$ zip -r -q -9 archive_compressed test_directory $ ls -l
We will create a ZIP archive called test_archive.zip which contains a directory test_directory which in turn contains 3 empty test files, test1.txt, test2.h, test3.c . To extract this archive, we will use the relax order.
1. Create a test folder containing the test files. After creating the test directory and files, verify that the files were created using ls before returning to the parent directory.
mkdir test_directory cd test_directory touch test1.txt test2.h test3.c ls cd
2. Create a ZIP archive called test_archive.zip which contains the test_directory. Verify that the archive was created using ls. the Zip*: French The command has two arguments, the name of the archive we want to create, and the source of the files to put in the archive.
zip -r test_archive test_directory ls
3. Delete the unarchived directory. If we extract the test_archive.zip in the parent directory that contains the test_directory we would be asked if we want to overwrite or rename the archive.
rm -r test_directory
4. Unzip it test_archive.zip file. Using relax without additional arguments, we see a detailed output report of each step of extracting the archive.
Often we will want to extract an archive to a different specified location. We can add the-D argument to the relax command to achieve this.
1. Extract the test_archive.zip in a specified directory. We can specify relative or absolute location. In this example, we extract the contents of the Music directory using a relative path. We could also pass an absolute path which provides the full path to the target location. After extracting the archive, move to the specified directory and use ls to verify that the archive was extracted.
unzip test_archive.zip -d ./Music
2. Change directory and list the contents to verify that the files were extracted correctly.
cd Music ls
With these few basic uses of the Zip*: French command, you now have many options when creating ZIP archives in the Linux terminal. Being able to select specific file types and being able to set the level of compression in the terminal emulator gives quick access to those powerful tools that are often hard to find in a GUI application.