PLAYMOL LOGO
 

Bash & misc

BASH is the language you apply when using the command line on Linux or Mac. There are many scripting languages, but in many situations BASH is all you need. Knowing how to take advantage of the BASH language can really be a time saver.

Here is a collection of code snippts and examples that I find useful and efficient (some require additional packages).

Content:

Create a series of folders and sub folder

I often perform several molecular dynamic simulations at different temperatures, and I like to organize the simulations in corresponding folders. If I where to do for example 20 simulations at 8 different temperatures, I would have to create a total of 160 folders. Doing this manually is of course time consuming and cumbersome. However, with just a few lines of BASH code this is done in seconds.

Here is an example where I create the temperature folders 275, 280, 285, 290, 295, 300, 305 and 310 and 20 subfolders for each temperature:

for T in $(seq 275 5 310);do
for sub in {1..20};do
mkdir $T/$sub
done
done
			
And that is all. Now it is easy to use the same idea to copy your MD jobs to each folder, submit them, and later extract results from them.

Do math from the command line

If you have ever tried doing math from the command line, you have probably tried to type something like:

echo "2.2 + 3.1"

This will however just return "2.2 + 3.1" and not "5.3" as we want. It is possible to do math within the BASH environment, but then we need to use bc:

echo "2.2 + 3.1" | bc

which returns "5.2" as we wanted it to. However, if you type:

echo "sqrt(3)" | bc

"1" is returned. This is because we need to specify how many decimals we want. Since the input is 3, this is taken as an integer, and that is also what is returned, even though we want a float. Use scale to specify how many decimals you want:

echo "scale=3; sqrt(3)" | bc

This will return "1.732".

ImageMagick: Resize & keep aspect

ImageMagick is perfect if you want to resize images. Here is an example to convert all png files to a new size with width 100 px without altering the aspect:

for i in *.png;do echo "$i"; convert "$i" -resize 100x "$i";done

100x resizes the width to 100 px and the height to the corresponding aspect. To resize based on the height, you just type x100 to obtain a heigth of 100 px:

for i in *.png;do echo "$i"; convert "$i" -resize x100 "$i";done

ImageMagick: Combine images

With ImageMagick you can combine several images side by side or on top of each other using layers.

Here is one example to combine two images side by side:

convert file1.png file2.png +append -quality 75 output.png

Here is one example to combine images on top of each other using layers:

convert -page 800x600 background.png -page +650+30 image1.png -layers flatten output.png

Here image1.png is placed on top of the image called background.png. The "+650+30" is the placement of image1.png relative to the first image (background.png). With '-page' you can place as many images as you like on top of each other.