How To Compress Files With Zstd Utility In Linux

Installing Zstandard on Linux

To start using the Zstandard compression tool, you first need to install it on your Linux system. The installation process varies slightly depending on the Linux distribution you are using.

On Debian, Ubuntu, and Linux Mint

To install Zstandard on Debian, Ubuntu, or Linux Mint, use the following command:

sudo apt install zstd

This command will download and install the Zstandard package along with any necessary dependencies.

On Fedora, Red Hat, CentOS, and AlmaLinux

For Fedora, Red Hat, CentOS, and AlmaLinux systems, the installation command is:

sudo dnf install zstd

This will install the Zstandard package using the dnf package manager.

On Arch Linux and Manjaro

For Arch Linux and Manjaro, use the following command to install Zstandard:

sudo pacman -S zstd

This command will install the Zstandard package using the pacman package manager.

Compressing Files with Zstandard

Compressing a Single File

To compress a single file using Zstandard, execute the following command:

zstd example.txt

This will create a compressed version of the file with the .zst extension. For example, example.txt will be compressed to example.txt.zst.

Compressing Multiple Files

You can compress multiple files at once by specifying each file separated by a space:

zstd example1.txt example2.txt example3.txt

Alternatively, you can use wildcards to compress all files of a certain type in the current directory:

zstd *.txt

This command will compress all .txt files in the current directory.

Setting Compression Levels

Zstandard allows you to adjust the compression level to balance between speed and compression ratio. The default compression level is 3, but you can specify a level from 1 to 19. For example, to use a higher compression level:

zstd -15 example.txt

For the highest compression levels (up to 22), use the --ultra option:

zstd --ultra -22 example.txt

This will use more memory but achieve better compression ratios.

Using Multiple Threads

To speed up the compression process, you can use multiple CPU cores by specifying the number of threads:

zstd --threads=4 example.txt

Alternatively, you can use the -T0 option to let Zstandard use all available CPU cores:

zstd -T0 example.txt

This can significantly speed up the compression process for large files.

Decompressing Files with Zstandard

Decompressing a Single File

To decompress a file compressed with Zstandard, use the -d option:

zstd -d example.txt.zst

Alternatively, you can use the unzstd command:

unzstd example.txt.zst

Both commands will restore the compressed file to its original state.

Compressing and Decompressing Directories

Compressing a Directory

To compress an entire directory, you can use the tar command in combination with Zstandard:

tar --zstd -cf example.tar.zst example/

This will create a compressed archive of the example directory with the name example.tar.zst.

Alternatively, you can specify additional parameters using the -I option:

tar --use-compress-program "zstd --threads=4 -19" --create --file example.tar.zst example/

This command sets the compression level to 19 and uses 4 threads for faster compression.

Decompressing a Directory

To decompress a directory that was compressed using the above method, use the following command:

zstd -d example.tar.zst | tar xvf -

This will extract the contents of the compressed archive to the current directory.

Additional Options and Features

Removing Original Files

After compressing a file, you can remove the original file using the --rm option:

zstd --rm example.txt

This will delete the original file after creating the compressed version.

Suppressing Output

To suppress the progress notifications and result summary, use the -q option:

zstd -q example.txt

This can be useful for scripts or automated processes where output is not needed.

By leveraging these features and commands, you can effectively use Zstandard to manage and compress your files efficiently on Linux systems.

Leave a Reply

Your email address will not be published. Required fields are marked *