Bz2 each file in a directory

I run centos, and I have about 1300 files in the folder, each of which must be separately inserted. What would be the easiest way to convey this?

+6
source share
4 answers

If all the files are in the same directory, then:

bzip2 * 

Enough. More robust approach:

 find . -type f -exec bzip2 {} + 

Which compresses each file in the current directory and its subdirectories and will work even if you have tens of thousands of files (using * will break if there are too many files in the directory).

If your computer has multiple cores, you can improve this by compressing multiple files at once. For example, if you want to compress 4 files at a time, use:

 find . -type f -print0 | xargs -0 -n1 -P4 bzip2 
+16
source

For bzip2 on a multi-core Mac, you can run the following command (when you are in the folder you want to use bzip)

 find . -type f -print0 | xargs -0 -n1 -P14 /opt/local/bin/bzip2 

This will automatically convert each file to a folder in which your terminal uses 14 processor cores simultaneously.

You can configure how many cores to use by editing

  -P14 

If you do not know where the bzip2 binary is located, you can output the following command to understand it

 which bzip2 

The output of this command is that you can replace

 /opt/local/bin/bzip2 

from

+2
source

From inside the directory: bzip2 *

0
source
 find PATH_TO_FOLDER -maxdepth 1 -type f -exec bzip2 -zk {} \; 
0
source

Source: https://habr.com/ru/post/893722/


All Articles