bash shell¶
open a terminal window to access the command-line (bash shell).
run a program¶
If a program is in your $PATH (environment variable defined in $HOME/.bashrc), then you can type the program name, and hit return:
$ matlab
If it’s not in your PATH, then email support-neuro@berkeley.edu
common commands¶
Below are some common commands
navigate:
cd go home (to your login directory)
cd .. go up a parent directory
cd /path/to/dir go to <dir>
cd path/to/dir go to path relative to here (no leading slash).
make:
mkdir dirname make a directory identified by 'dirname'
list:
ls list contents of current directory
ls -a include files with "." (dot files)
ls -l list contents in long format
ls -lrt list contents, sort by modified time in reverse order
ls -lR list contents recursively
info:
file filename show the content type of 'filename'
view:
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/rename:
mv src dest rename file; however, if 'dest' is a directory, then 'src' is moved to 'dest'
remove:
rm filename delete filename
rm -r dir remove a directory and its contents
search:
find . -name "*.nii" find files in current directory that end with '.nii'
process management:
pgrep name print jobs with 'name'
pkill name kill jobs with 'name' (only your jobs will be terminated).
Show system resources:
top show process information on this system
(type 'q' to quit)
- Change password:
See: change password
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.
Start a text editor:
$ nano
Enter the following text, and save as ‘simple.sh’:
#!/bin/shdatesleep 20Make the script executable:
$ chmod a+x simple.sh
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’).