bash shell

using the command line

To access the command line from your NX session, open a terminal window: right-click on the background, and select ‘Open Terminal Here’:

../../_images/terminal-menu.png

Alternatively, there is a shortcut icon in the lower left of the screen:

../../_images/terminal-shortcut.png

locating software

Most software is located in /usr/local. Use the ‘which’ command to locate if (and where) a progam is installed, for example:

$ which matlab

bash shell script

A shell script is a series of commands saved to a file for later execution.

You can create a script with a text editor. Text editors available on the cluster include:

  • gedit: simple editor with a graphical interface

  • nano: simple editor with a curses-based interface

  • emacs: advanced editor with a graphical interface

  • vi: advanced editor with a text interface

Below is an example of a script that prints the date, and then sleeps 20 seconds.

  1. Start a text editor:

    $ nano
    
  2. Enter the following text, and save as ‘simple.sh’:

    #!/bin/sh
    date
    sleep 20
  3. Make the script executable:

    $ chmod a+x simple.sh
    
  4. Run the command at the shell prompt:

    $ ./simple.sh
    

bash iteration

A common use of shell programming is to perform a task multiple times, iterating over a list.

In this example, we use a ‘for’ loop to run the ‘echo’ command 5 times. As a result, the value of ‘i’ is printed (echo’d) to the screen, where i=1, i=2, i=3, i=4 and i=5. The for loop is terminated with the ‘done’ statement.

You may copy and paste the lines below into a terminal window. Alternatively, copy them into a file, and run it as a shell script:

for i in 1 2 3 4 5; do
    echo "$i"
done

The ‘seq’ command prints a sequence of numbers, and has options to define the increment, pad with zeros, etc. In this example, we evaluate the numbers 0 to 20 by increments of 4:

j=0
for i in `seq 0 4 20`; do
    # print value of i
    echo "i is $i"
    # add i+j
    ((j = $j + $i))
    # print value of j
    echo "j:$j"
done

you may copy and paste the code directly into a terminal window, or copy them into a file, and run it as a shell script. To learn more about seq, run ‘man seq’ (to quit the ‘man’ command, type ‘q’).

common commands

Below are some common unix commands that you can type from a terminal window

Change Working Directory:

cd                  go to your login (home) directory
cd ~username        go to username's login (home) directory
cd ..               go up one directory level from here
cd ../..            go up two directory levels from here
cd /path/to/dir     go to <dir>
cd path/to/dir      go to path relative to here (no leading slash).

Make a directory:

mkdir   dirname        make a directory identified by 'dirname'

List files:

ls          list contents of current directory
ls -a       include files with "." (dot files)
ls -l       list contents in long format
ls -rt      list contents, sort by time modified in reverse order

Determine a file type:

file filename           show the content type of 'filename'

Display file contents:

cat   filename          show contents of filename
less  filename          show contents of filename one page at a time
head  filename          show first 10 lines of filename
tail  filename          show last 10 lines of filename
head -50 filename       show first 50 lines of filename

Move (or rename) a file or directory:

mv srcfile destfile    rename srcfile to destfile
mv srcfile destdir     move a file into the destdir directory
mv srcdir destdir      rename srcdir to destdir; if destdir exists, then move to srcdir to destdir
mv -i srcdir destdir   same as above, but prompt before overwriting

Copy a file or directory:

cp srcfile destfile    make a copy of srcfile called and call it destfile
cp srcfile destdir     make a copy of srcfile, and place it in the destdir directory
cp -R srcdir destdir   copy srcdir (and contents) to destdir
cp -i src dest         same as above, but prompt before overwriting

Remove a file:

rm filename         remove (delete) filename
rmdir dir           remove an empty directory
rm -r dir           remove a directory and its contents
rm -i filename      remove filename, but prompt before deleting

Redirection:

cmd1 | cmd2             "pipe" output from cmd1 to input of cmd2
command > filename      direct output of command to filename, replacing contents of filename

Search files for text string:

grep string filename        show lines containing string in filename
grep string filelist        show lines containing string in any file in filelist
grep -v string filelist     show lines not containing string
grep -i string filelist     show lines containing string, ignore case
grin pattern directory      recursive through directory; show lines of all ascii files that match pattern

Limit search with grep:

ls | grep pattern       show output of ls that matches pattern

Search directories for file name:

find . | grep pattern       recurse through current directory; show file names that match pattern
find ~ -name "*.nii"        recurse through home directory; show file names that end with '.nii'

Compress files or directories:

Show running process:

pgrep processname               show processes called 'processname'
ps -ef | grep username          show all processes owned by username

cssh pgrep processname          show jobs called 'processname' on all workstations
cssh ps -ef | grep usrname      show jobs owned by username on all workstations

Terminate a process that you own (running with your username):

ctrl-C              kill foreground process
kill pid#           kill process identified by pid number (see 'ps' above)
pkill processname   kill process identified by 'processname' (see 'pgrep' above)

Show system resources:

nxload              show resources available on all workstations
top                 show process information on this system
top -u username     show process information of username on this system

User information:

finger username     show information about username
w                   show who is logged in to this system

Show Sun Grid Engine queue:

qstats | less                   display SGE queue information one page at a time

Change password:

See common commands