Site Feed

Monday, September 15, 2008

Convert a Text File to HTML

While considering how to make my shell scripts look better on the web, I started looking at for programs that would convert source code to html.

Then I thought, vim already has syntax highlighting. I wonder if it has a way to convert to html.

Sure enough, it can be done with a single command:

:runtime! syntax/2html.vim

Convert Text File to HTML
:runtime! syntax/2html.vim : convert txt to html
:h 2html : help about 2html

:wq! : write the new file

The current file is converted, and an .html extension is added. Once the file is saved, it can be opened in a browser.

For more vi tips, check out:

And the official vim site:,

Here is a beautiful example of C source code that was quickly converted to HTML, while preserving all of vim's color coding:

Backup Logs with a Shell Script

Here is a very simple way to backup and compress subdirectories. Normally you would want to copy them to a remote location in case something happens to the original drive, but in this case the concern is that disk space is filling up.

Suppose you have a program that is generating log files every day, let's say they are key performance indicators (kpi) for some system that you are testing. The log files are pure text, but after a while, they take up a lot of space. One way to compress text is to use gzip, but since tar has an option (-z) to tar and gzip all at once, it may be one of the easiest solutions.


# compress logs in /opt/multicast_kpi/record
# by ScottM, 20080613

# Set variables
TODAY="`date +%m%d%Y`"

# move to working directory

# For each subdirectory, make a .tgz file of it's contents, and then delete the originals.
for dir in `ls -1 $ARCHIVE_DIR | grep -v ".tgz" | grep -v "${TODAY}"`
do tar -C ${ARCHIVE_DIR} -czvf ${dir}.tgz ${dir}
# Note that the archive directory is hard-coded here:
# We don't want to use rm -rf just anywhere...
# Even then, we still verify that the directory exists before running rm
[ "${dir}" != "" ] && [ -d ${ARCHIVE_DIR}/${dir} ] && rm -rf /opt/multicast_kpi/record/${dir}

# go back to previous directory
cd -

The results will be:

ls -1 /opt/multicast_kpi/record/

ls -1 /opt/multicast_kpi/record/

Then the program can continue to create subdirectories of any name, and place log files there. Our script does not care what name the subdirectory has. Of course, as some point the .tgz files may fill up the disk, so that is another problem you will have to deal with.

For more fun, take a look at the logrotate command, which can be used to rotate, compress, and mail system logs.