From MPIA Students' home page

Science: Bash-commands

Here is a list of the most useful bash programmes and commands. If you're not familiar with the syntax then have a look at the bash tutorial. Some programmes might not be installed on your computer.

FEEL FREE TO ADD SOMETHING WHEREVER YOU WANT!

Computer performance

Checking CPU

You can check the computer performance with the top command.
There are several top-clones:
htop is a "nice-n-shiny" color-ascii version, where you can easily setup the output.
atop monitors only active processes, while ignoring sleepers and zombies. Wherefore it is a good tool for the advanced user to analyze the current load for your system.

$ top
$ htop
$ atop

A useful tool for laptop users is powertop it monitors the wakeup events of your CPU while the system is idle. The more wakeups per seconds you have the more battery power your system consumes. For more details check out Less Watts. (Even through it is mend for Thinkpads it gives also general hints for improving your battery uptime.)

Setting priorities

nice gives programmes higher/lower priority, so that CPU expensive programmes don't block your computer. With

$ nice -19 command

you can lower the priority of command to 19 (which is the lowest value, default is 0). This means that it consumes your CPU only in case not other programmes need it. In case you want to give a running programm a different priority use renice

$ ps aux | grep command
user  7917  0.1  0.4  26860  8408 pts/0    SN+  10:46   0:00 command
$ renice 19 -p 7917

First you have to query for the process ID of the command with ps, and then you can renice it.

Killing processes

If you have a stuck process you can kill it if you know the process ID.

$ ps aux | grep command
user  7917  0.1  0.4  26860  8408 pts/0    SN+  10:46   0:00 command
$ kill 7917

If it still doesn't react you can be a more "demanding" by adding the option -9.

$ kill -9 7917

Another option is to kill all programmes (e.g. firefox) with a certain name via killall

$ killall firefox

General file handling

You looking for a fast "semi-graphical" command-line file-browser, like good old DOS-Norton-Commander? Try the Midnight Commander:

$ mc

You want to move/copy files according to several pattern which can be only matched by wildcards? Try multiple move/copy ('mmv', 'mcp'). Here an example. We have the following files:

$ ls -1 *fits
FORS1.2006-08-01T03_19_40.550.box1.fits
FORS1.2006-08-01T03_19_40.550.box2.fits
FORS1.2006-09-03T00_56_57.600.box1.fits
FORS1.2006-09-03T00_56_57.600.box2.fits
FORS1.2006-09-03T00_59_08.625.box1.fits
FORS1.2006-09-03T00_59_08.625.box2.fits

So respectively to the box-keyword the files should be move to the folders Box1/Box2. Furthermore we want to switch date and time:

mmv "FORS1.*T*.box?.fits" "Box#3/FORS1.#2T#1.box#3.fits"

As a result you see:

$ ls -1 Box1/
FORS1.00_56_57.600T2006-09-03.box1.fits
FORS1.00_59_08.625T2006-09-03.box1.fits
FORS1.03_19_40.550T2006-08-01.box1.fits

$ ls -1 Box2/
FORS1.00_56_57.600T2006-09-03.box2.fits
FORS1.00_59_08.625T2006-09-03.box2.fits
FORS1.03_19_40.550T2006-08-01.box2.fits

Every wildcard in the source-pattern (*? ...) is represented by (#1, #2, 3# ...) in the order as they occur in the source pattern. BTW 'ls -1' is the same as 'ls', but only gives one entry per line.

Edit lots of files

Useful e.g. for renaming variables in a code with lots of files: Search for all files with name filename in current directory (including all sub-directories) and replace all occurences of aaa in them with bbb.

$ find . -name filename -exec sed -i 's/aaa/bbb/g' {} \;

Find files with a certain pattern which are in different subdirectories and copy them to another folder in your home.

$ find . -name '*pattern*.filetyp' -exec cp {} $HOME/Anotherfolder/ \;

$HOME contains the location of your home directory for almost every Linux distribution. You can use also '~' in a path. There are several placeholders:
. is the the current dir
.. is the dir one level up
~ is the home
$OLDPWD contains the last dir before changing to the current one. (Can be invoked by 'cd -')

If only a few subdirs matching a certain pattern should be searched, try:

$ find . -wholename '*dir_pattern*/*file_pattern*' -exec cp {} $HOME/Anotherfolder/ \;

FITS file header

This commands reads the complete fits header

$ imhead test.fits
SIMPLE  =                    T /
BITPIX  =                  -32 / FITS BITS/PIXEL
NAXIS   =                    2 / NUMBER OF AXIS
NAXIS1  =                  679 /
NAXIS2  =                  603 /
...

It is also possible to query or change keywords

$ gethead EQUNIOX test.fits
1950.
$ sethead EQUINOX=2000. test.fits
$ gethead EQUINOX test.fits
2000.
Tags: BASH, programming, science
Retrieved from http://www2.mpia-hd.mpg.de/home/STUDENT/wiki/index.php?n=Science.Bash-commands
Page last modified on January 23, 2011, at 08:24 PM