linux

Linux breaking lines to fixed length

Break line in fixed width with the linux fold command.

I keep forgetting this command, so here goes.

The Linux 'fold' command, can be used to wrap each input line to fit in specified width.

If width is not specified using -w option, by default, 'fold' breaks lines wider than 80 columns. The output is split into as many lines as necessary.

Replacing text in a directory tree

Sometimes you want to replace one piece of text with another in all files in a whole directory tree.

*nix and sed to the rescue.


find ./ -type f -exec sed -i ’s/queryString/replaceString/g’ {} \;

Tar selected files based on file name

To include only a set of files with tar, you can use the following one liner. This example will tar all files with the java extension in the archive with the name test.


find . -name '*.java' | tar czvf test.tar.gz --files-from -

Determine uncompressed size of GZIP file

For some applications it is useful to determine the uncompressed size of a file that has been compressed by the gzip algorithm. From the command line this can be done by using the -l option of the gzip program. But this is less straightforward using the Java API's. The GZIPInputStream class does not provide a method to query the original file size. However the information is present in the GZIP file for files that were originally smaller than 4 Gb and can be extracted.

Timing runtime in Linux

When writing new software or implementing new algorithms it is always nice to know how long it takes to process a certain data set. Alternatively it is just good to know how much time a program consumed when you let it run overnight. Did it take till 7 am in the morning or was it finished five minutes after you left your desk.

Linux has a very straighforward way to time your applications. The command is simply called time.

Simply put time in front of your program or script when calling in and you well get some information regarding the runtime it took.

Recursively renaming files in Linux

This tutorial explains how to rename all files in a directory and its subdirectories. Or just how to rename multiple files at once in Linux.

This can be done with one very simple command:

rename some other `find . -name '.*'`

This will replaces all occurences of 'some' in file names with 'other'.

A simple example: rename all htm files to html.

rename htm html `find . -name '*.html'`

The rename command takes 3 arguments:

  1. thing to replace
  2. thing to replace it with
  3. a list of files to apply it to
Syndicate content