Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 322
Steven Kull Avatar asked Jul 27 '11 21:07

Steven Kull


People also ask

How do I grep in BZ2 files?

All options specified are passed directly to grep. If no file is specified, the standard input is decompressed if necessary and fed to grep. Otherwise, the given files are decompressed (if necessary) and fed to grep. If bzgrep is invoked as bzegrep or bzfgrep, egrep or fgrep is used instead of grep.

What command decompresses a bzip2 file?

bz2 file is a Tar archive compressed with Bzip2. To extract a tar. bz2 file, use the tar -xf command followed by the archive name.

What does bzip2 command do in Linux?

bzip2 command in Linux is used to compress and decompress the files i.e. it helps in binding the files into a single file which takes less storage space as the original file use to take. It has a slower decompression time and higher memory use.


2 Answers

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
like image 178
sagi Avatar answered Sep 26 '22 03:09

sagi


To bzip2 on a multi-core Mac, you can issue the following command (when you're inside the folder you want to bzip)

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

This will bzip every file recursively inside the folder your terminal is in using 14 CPU cores simultaneously.

You can adjust how many cores to use by editing

 -P14

If you don't know where the bzip2 binary is, you can issue the following command to figure it out

which bzip2

The output of that command is what you can replace

/opt/local/bin/bzip2

with

like image 24
Shaheen Ghiassy Avatar answered Sep 22 '22 03:09

Shaheen Ghiassy