Steven Kull Steven Kull - 7 months ago 15
Linux Question

Bz2 every file in a dir

I am running centos and I have around 1,300 files in a folder that each need to be bzipped individually. What would be the easiest way of handing this?


If all the files are in a single directory then:

bzip2 *

Is enough. A more robust approach is:

find . -type f -exec bzip2 {} +

Which will compress every file in the current directory and its sub-directories, 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, then you can improve this further by compressing multiple files at once. For example, if you would like to compress 4 files concurrently, use:

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