Linux Commands

How to compress files with zstd from the command line or bash script

In the modern world, we interact and work with data everywhere and every day. Depending on the needs at hand and the data requiring processing, storage, and computing power can quickly become limited resources.

To solve this problem, developers developed compression algorithms and tools capable of compressing data in real-time, reducing the size and processing power required. One of these tools is Zstandard, commonly known as Zstd.

Zstd is a free, open-source, real-time compression algorithm developed by Yann Collect, an employee at Facebook. Zstd is very fast and offers outstanding compression ratios. It is a lossless compression algorithm written in C but has API implementations in other popular programming languages such as Python, Java, C#, JavaScript, and many more. It also provides in-memory compression and decompression functions.

To check if Zstd supports your desired language, check the resource provided below:

https://facebook.github.io/zstd/

If you wish to look at benchmarking information about Zstd, use the link below:

https://github.com/facebook/zstd

This tutorial will show you how to compile and install the Zstd tool in Linux, then use it to perform data compression and decompression.

How to Install Zstd

To use Zstd, we need to install it by compiling from the sources. Depending on the system you are running and the configuration, you may need to install dependencies and tools to perform the compilation successfully.

Start by updating your system and installing GNU make using the command:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get -y install build-essential wget tar

Once we have all the tools installed, we can download the source files and compile them. Start by navigating where you have read, write, and execute permissions. ~/Desktop

cd ~/Desktop

Next, use wget to download the files into the directory.

wget https://github.com/facebook/zstd/releases/download/v1.4.9/zstd-1.4.9.tar.gz

Now unarchive the download file and navigate into the directory using the commands as:

tar xvf zstd-1.4.9.tar.gz
cd zstd-1.4.9

The final steps are to install Zstd by using make and make install inside the Zstd directory.

sudo make
sudo make install

Once the compilation and installation complete successfully, you can start using Zstd on your system to compress and decompress files.

How to Use Zstd

Zstd does not defer from popular compression and decompression methods at all. Although the underlying technology and implementation are different from other tools, compressing a file with Zstd is similar to tar and gzip syntax.

How to compress a file

To compress a file, call the zstd command followed by the -z flag, which tells zstd to do the compression, and finally, the name of the file to compress.

For example, the command below compresses the system-backup file into a .zst file.

$ sudo zstd -z system-backup

The command output as shown below:

system-backup :100.00%   (1821109 => 1821164 bytes, system backup.zst)

$ ls system-backup

You should see a file with .zst extension as:

system-backup.zst

Once the command executes, the file gets compressed and creates a filename .zst file that you can decompress.

Compress and remove the source file

As you can see from the above command, the source file does not get removed by default upon compression. You can specify to remove the source files by using the –rm flag:

$ sudo zstd -z --rm system-backup
system-backup        :100.00%   (1821109 => 1821164 bytes, system-backup.zst)
$ ls
system-backup.zst

Specifying the –rm flag automatically removes the source file.

Get file info

To display related information about the Zstd compressed file, you can use the -l flag followed by the file name. The displayed information includes the file size, compression ratio, and the file checksum

$ zstd -lv system-backup.zst
*** zstd command line interface 64-bits v1.4.9, by Yann Collet ***
system-backup.zst # Zstandard Frames: 1
Window Size: 1.74 MB (1821109 B)
Compressed Size: 1.74 MB (1821164 B)
Decompressed Size: 1.74 MB (1821109 B)
Ratio: 1.0000
Check: XXH64

Specify the compression level

To explicitly specify the compression level, use the – where the level is a value ranging from 1 – 19. The default compression level is 3. You can also unlock higher compression levels, i.e., level 20 – 22.

NOTE: The higher the compression level, the higher memory usage.

For example, to compress a file with a compression level of 10, use the command:

zstd -z -10 --rm system-backup

Specify Compression Speed.

Zstd also allows you to set the compression speed ranging from 1 – to. The compression speed is inversely proportional to the compression ratio. The default compression speed is 1, and the higher the value, the faster the compression speed.

For example, to use the maximum compression speed, use the command:

sudo zstd -z --fast=10 system-backup

Specify Compression Format

You can also specify the compression format to use if you do not like the default zst compression. Formats include zstd, gzip, xz, lzma, and lz4.

Use the –format flag and specify the format as:

zstd -z --format=gzip system-backup

Compress file list

Suppose you have a list of files you would like to compress all at once. Zstd allows you to pass a file containing a list of files and compresses them recursively.

For example, a file list.txt containing the files

/home/user/mysql-backup
/backups/config
/home/Desktop/media
/sync/2021/users

Once you save the file, you can pass the list to Zstd with all other options to perform on the files.

sudo zstd -z -v --rm --filelist list.txt

This command will compress all the files specified in the text file and remove them upon completion.

Decompress a file

To decompress a file, you can use the -d flag with the zstd command or simply use the unzstd command to decompress.

For example:

sudo unzstd system-backup.zst
sudo zstd -d system-backup.zst

Verify file integrity

To test the integrity of a zst compressed file, use the -t flag as shown in the command below:

sudo zstd -t system-backup.zst

Conclusion

As we can see from the examples, Zstd is a powerful compression algorithm with numerous use cases. To learn more about how it works and its implementation, check the man page and experiment.

Thanks for reading.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list