Friday, October 29, 2010

Bash script for bulk renaming files

In Linux, logs should be compressed with bzip2 at highest compression.

Usually they are compressed with gzip and filenames end in .gz . To achieve bzip2 compression, I put this in the logrotate configuration file:

compresscmd /bin/bzip2
compressoptions "-9"

That operation was a success but the patient almost died. The files did not get the correct file suffix that should have been .bz2 . So I wrote a script to do that automatically once and for all.

for SKRA in *.gz ; do
[ "$(file --mime-type --brief $SKRA)" == 'application/x-bzip2' ] && ( mv $SKRA ${SKRA/.gz/.bz2} );
done



In order to ensure that the files would get the right filename, I put this into the logrotate configuration:

compresscmd /bin/bzip2
uncompresscmd /bin/bunzip2
compressoptions "-9"
compressext .bz2

No comments:

Post a Comment