You are on page 1of 19

UNIX essentials (hands-on)

overview: Unix, tcsh, AFNI the directory tree basic shell commands (class practice) running programs the shell (using the T-shell)

command line processing special characters command types shell, array and environment variables wildcards shell scripts shell commands pipes and redirection ! commands

special "iles
1

Overview: Unix, T-shell, AFNI

Unix

a type o" operating system (a standard), "irst developed in #$%$ e&amples: !olaris,

pen!olaris, Iri&, 'IX, ()-UX,

! X, *inu&, +ree,!-

actually, *inu& and +ree,!- are not Uni& compliant, but are very similar has graphical environment, but a strength is in command-line capabilities hundreds o" commands, minimum, thousands on most systems

tcsh (T-shell)

a Uni& shell: a command-line interpretter

when user types a command and hits Enter, the shell processes that command

.ust one o" the common Uni& programs (a single "ile: /bin/tcsh) other Uni& shells: sh, bash, csh, 0sh, 1sh has own synta& and sub-commands not as power"ul as bash, but more simple and readable

AFNI a suite o" data analysis tools more than 234 programs, scripts and plugins "ree and open source
2

Overview: Unix, T-shell, AFNI

- separate commands and synta&

Unix : sample commands and synta& commands: ls, cat, less, mv, cp, date, ssh, vi, rm synta&: variables ($), 5uotes (', ", `), wildcards ( , !, "#), pipes ( $ ), redirection ( % ) comments: part o" any Uni&-based system (e6g6 &'laris, (in)x, O& *) command help "rom 7man7 pages (or a boo0), e6g6 man less tcsh (T-shell) : sample commands and synta& commands: cd, set, setenv, i+, +'reach, alias, hist'r, synta&: home directories (-), history (.), .obs (/), redirecting stderr (%0) comments: single installed program, command help "rom 7man tcsh7 (or a boo0) AFNI : sample commands and synta& commands: a+ni, s)ma, 1dcalc, a+ni2pr'c3p,, 1d4ec'nv'lve synta&: sub-bric0 selection ("$#) - note: these characters appear elsewhere comments: installed suite o" programs, command help "rom -help output e6g6 a+ni2pr'c3p, -help e6g6 a+ni2pr'c3p, -help $ less
3

The Directory Tree


(the organi1ation o" the "ile system)

directories contain "iles and/or directories 5 : means either the root directory, or a directory separator

consider 5h'me5a+ni)ser5AFNI2data6

&)r+4ata5&U7A

getting to &)r+4ata5&U7A re5uires starting "rom s)ma2dem'

an 8absolute8 pathname begins with 757, a 8relative8 pathname does not

a relative pathname depends on where you start "rom the relative pathname "or the parent directory is 7..7

every directory has a parent directory

what does 8cd 338 do9 what does 8cd 38 do9

the relative pathname "or the current directory is 7.7

consider "35r)n2this2script", "cp -5+ile 3", "ls 335s)ma2dem'" cd,

many commands can be used to return to the home directory (o" 8a+ni)ser8)

cd 5h'me5a+ni)ser,

cd $8O79,

cd -,

cd -a+ni)ser

note the : special characters, 7-7 and 7$7

while you wor0, 0eep your location within the directory tree in mind
4

Basic Shell Commands: open a terminal window and practice


#6 Upon opening a new terminal window, what directory am I in9 :6 'ppro&imately how many "iles and directories are here9
ls -l 5:in5tcsh, ls -l a:in5a+ni, ls -l,

(pwd)

(ls, ls -l, ls -al)

;6 (ow big are these programs (as "iles): tcsh, a"ni, 6cshrc, s##6proc6+T9
ls -l 3cshrc, wc s;;3pr'c3FT cd AFNI2data65FT2anal,sis,

26 The last : are actually scripts, loo0 at them6

(cat, <edit, nedit) (less s;;3pr'c3FT)

36 <hat polynomial order is used "or the baseline in ;d-econvolve in s##6proc6+T9 !earch "or 8polort8 in the ;d-econvolve command6 (consider 0eystro0es in less:
Enter, Space, :, <, =, h, 5, n, N, >

--= down line, page, up page, go to top, bottom, help, search, ne&t, ne&t-up, 5uit) %6 <hy are the line continuation characters (7?7) use"ul9 (less s;;3pr'c3FT) --= "or readability, note: must be *'!T character on line >6 (ow many runs are processed in the +'reach r)n loops9 ("ind 8set r)ns @8) ?6 <hat are the arguments to the withinAB "unction in 1dcalc9
1dcalc -help $ less

(use 757 and 7n7 to search "or occurances o" 7within7)

$6 I" we run a+ni, how can we still type commands (without opening another terminal)9 (ctrl-C, :<) (also try: a+ni 0)
5

Running Programs

a program is something that gets 8e&ecuted8, or 8run8 the "irst element o" a command line is generally a program ("ollowed by a space) most shells are case sensitive when processing a command command e&amples (options usually start with a 7-7) : 5:in5ls $8O795AFNI2data6 c')nt -di<its D ; ;E script: an interpreted program (interpreted by some other program) e6g6 shell script, .avascript, perl script, a"ni startup script recall: less -5AFNI2data65FT2anal,sis5s;;3pr'c3FT create a script (te&t "ile) containing a "ew commands: (<edit m,3script)
ech' Fhell' thereG ls -a c')nt H ;;

e&ecute the script in a "ew ways


tcsh m,3script :ash m,3script 35m,3script chm'd HII m,3script 35m,3script

@--- script should start with 7J.5:in5tcsh7, "or e&ample


6

The Shell ("ocusing on the T-shell)


a shell is a command interpreter (case and synta& sensitive) e&amples: tcsh, csh, sh, :ash, Ksh, Csh, wish, tclsh, rsh, ssh command: ech' $E the T-shell: 5:in5tcsh

an enhanced A-shell (csh), which has A programming style synta& ! " # $

Command Line Processing (simpli"ied outline):


#) :)

evaluate special characters, such as: ~ $ & * ? decide which program to e&ecute (more on this later)

absolute pathname9 alias9 shell command9 in the $%ath?

;) 2)

e&ecute appropriate program, passing to it the parameter list save the e&ecution status in the $status variable (4 is considered success) tcsh has automatic "ilename completion using the Tab 0ey

type 8ls suma8 and hit the Tab 0ey, watch what happens, and hit Enter type 8ls &'8 and hit the Tab 0ey, note what happens note: this re5uires setting the shell variable, (ilec
7

S%ecial Characters (some o" them, and some o" their uses)
- : the current user7s home directory (e6g6 5h'me5a+ni)ser), same as $8O79 $ : used to access a variable (e6g6 $path) & : used to put a command in the bac0ground (e6g6 a+ni 0) : wildcard, matching 1ero or more characters (e6g6 ech' AFNI2da ) ! : wildcard, matching e&actly one character (e6g6 ls AFNI2data!) ? : command line continuation (must be the last character on the line) ' : the shell will not evaluate most special characters contained within these 5uotes (e6g6 echo '$8O79' : will output $8O79, not 5h'me5a+ni)ser) (e6g6 1d:)cKet -pre+ix small2+)nc '+)nc2slimL'ri<"E,D33M#') " : the shell will evaluate $varia:les and `c'mmands` contained within these (e6g6 ech' "" # m, h'me dir is $8O79") (e6g6 ech' "the n)m:ers are 'c')nt H ;D'" ) ` : e&ecute the command contained within these 5uotes, and replace the 5uoted part with the output o" the contained command (e6g6 ech' "the n)m:ers are `c')nt H ;D`" )
8

Command Ty%es

the shell must decide what type o" command it has: pathname "or a program: e&ecute that program alias: apply any alias(es) then start over (decide on which program to run) shell command: part o" the 5:in5tcsh program

chec0 the $NAT8 directories "or the program

consider the commands:


5:in5ls AFNI2data65a+ni ls AFNI2data65a+ni cd AFNI2data65a+ni wc -5AFNI2data65a+ni5epi2r;2ideal3;4

the 8which8 command shows where the shell gets a command "rom:
which ls which cd which wc

Shell )aria*les: The P&T+ )aria*le


a list o" directories to be searched "or a given program to be run "rom the $path and $NAT8 variables are identical, but are represented di""erently commands: ech' $NAT8
ech' $path cat -53cshrc
9

Shell )aria*les

shell variables are variables that are stored in, and a""ect the shell all variables are stored as strings (or as arrays o" strings) a variable is accessed via the 7$7 character the 7ech'7 command: echo the line a"ter processing any special characters

command: ech' m, h'me dir, $8O79, h'lds -5 without arguments: 7set7 displays all variables, along with any values 7set7 ta0es a list o" variables to set, possibly with values consider the commands:
set +''d ech' $+''d set +''d @ picKle ech' $+''d set +''d eat @ ch'c'late d'n)t (emphasis: "ood eat B chocolate donut) set set +''d @ eat ch'c'late d'n)t set +''d @ "eat ch'c'late d'n)t" ech' $+''d
10

the 7set7 command: set or assign values to one or more variables


variables can be assigned the result o" a numerical computation using the 7,7 command, however only integer arithmetic is allowed commands: set val)e; @ ;H
O val)eD @ $val)e; ech' val)eD @ $val)eD D L 6

&rray )aria*les

array variables are set using A B consider the commands:


set st)++ @ A ;; ;D ;1 seven ;I B ech' $st)++ ech' $st)++";# ech' $st)++"D-M# ech' $st)++"P# set st)++ @ A hi $st)++ $+''d B ech' $st)++ ech' $path cat -53cshrc

11

-n.ironment )aria*les

similar to shell variables, but their values will propagate to children shells by convention, these variables are all upper-case (though it is not re5uired) similarly, shell variables are generally all lower-case set environment variables using 8setenv8 (as opposed to the 8set8 command) without any parameters, the 8setenv8 command will display all variables the 8setenv8 command will only set or assign one variable at a time the "ormat "or the command to set a value is (without any 7B7 sign):
setenv QARIAS(9 val)e

commands:
setenv 7T2NA79 9lvis ech' $7T2NA79 ech' $path ech' $NAT8 ech' $8O79 setenv

12

/ildcards

used "or shell-attempted "ilename matching special characters "or wildcards:


, !, ", #, U

: matches any string o" 1ero or more characters (special case: a lone C will not match "iles starting with 767)
!

: matches e&actly one character

"# : matches any single character within the s5uare brac0ets "U# : matches any single character DXAD)T "or those within the brac0ets

commands (run "rom the AFNI2data659NI2r)n; directory):


ls ls ls ls ls ls ls ls

-a P 13dcm P E 13dcm P EE!13dcm P EE"D1#3dcm P EE"UD1#3dcm

13

Shell Scri%ts

a te&t "ile, a se5uence o" shell commands the 7 7 character can be used "or line continuation ("or readability) "or that purpose, it must be the last character on the line (including spaces) e&ecuting shell scripts, ; methods: ;B 35+ilename : (sa"est) e&ecute according to the top "J.pr'<ram8 i" no such line, usually e&ecuted via :ash (a potential error) the "ile must have e&ecute permissions (see !ls -l7, 7chm'd7) DB tcsh +ilename : e&ecute as t-shell commands 1B s')rce +ilename : e&ecute using current shell a""ects current environment this method should be used only when that is the intention (e6g6 3cshrc) recall -5AFNI2data65FT2anal,sis5s;;3pr'c3FT
create a scri%t 0te1t (ile2 called m,3script containing a (e3 commands

recall: e&ecute the script in a "ew ways


tcsh m,3script :ash m,3script 35m,3script chm'd HII m,3script 35m,3script

@--- script should start with 7J.5:in5tcsh7, "or e&ample


14

Some Shell Commands (handled by the shell)


cd pwd set O alias :< +< exit setenv s')rce

: change wor0ing directory : display the present wor0ing directory : set variables or assign string values to variables : set a variable to the results o" an integral computation : display or create an alias (e6g6 alias hi 'ech' hell' there' ) : put a process in the bac0ground (usually a"ter ctrl-1) : put a process in the "oreground : terminate the shell : set environment variables : e&ecute a script within the current shell environment

special 0eystro0es (to use while a process is running) ctrl-c : send an interrupt signal to the current process
ctrl-C

: send a suspend signal to the current process

15

4ore Shell Commands: *asic (lo3 control

commands: i+, else, endi+, while, end, +'reach


i+ A $)ser @@ "elvis" B then ech' 'the Kin< lives' endi+ set val)e @ I set +act @ ; while A $val)e % E B O +act @ $+act $val)e O val)e -@ ; end ech' I +act'rial @ $+act +'reach val)e A ; D 1 +')r ei<ht ;; B ech' the c)rrent val)e is $val)e end +'reach +ile A I3 1 B ls -l $+ile end

16

Pi%es and Redirection


% : redirect program output (std')t) to a "ile

e6g6 1dmer<e -help % 1dmer<e3help


1dmer<e -picKle % 1dmer<e3help %0 : redirect all output (both std')t and stderr) to a "ile

e6g6 1dmer<e -picKle %0 1dmer<e3picKle e6g6 tcsh m,3script %0 script3')tp)t


%% : append program output to a "ile

e6g6 ech' "remem:er t' +eed the cat" %% script3')tp)t


$ : pipe standard output to the input o" another program

e6g6 1d4ec'nv'lve -help $ less


$0 : include stderr in the pipe

e6g6 tcsh -x m,3:i<3script $0 tee script3')tp)t run the script (echo commands to terminal be"ore e&ecuting) send all output to the tee program the tee program duplicates its input, sending the output to both the terminal and the given "ile (script3')tp)t) you can see the output, but it is also stored "or "uture analysis
17

Some 5S Commands
ls * cat : list the contents o" a directory : concatenate "iles to the terminal (print them to the screen)

* m're : a "ile perusal program - view "iles one page at a time * less : a better "ile perusal program (type less, get more) echo : echo command to terminal window man : on-line manuals "or many ! commands (and library "unctions) - this uses a 8less8 inter"ace to display the in"ormation - e6g6 consider man on : ls, less, man, tcsh, a+ni * head : display the top lines o" a "ile (de"ault B #4) - e6g6 6din(o (unc7slim8orig $ head 9:; * tail : display the bottom lines o" a "ile (de"ault B #4) - e6g6 tail ideal2r;3;4 * wc cp mv rm : word count - count characters, words and lines (o" a "ile) : copy "iles and directories to a new location : rename a "ile, or move "iles and direcotories : remove "iles and/or directories (,D A'ED+U* - no recovery) - e6g6 rm V)nK3+ile - e6g6 rm -r :ad3direct'r, C denotes a 7"ilter7 program, which can ta0e input "rom a "ile or "rom stdin
18

* <rep : print lines "rom a "ile that match the given pattern e6g6 <rep path -53cshrc e6g6 ls -5a:in $ <rep -i v'l e6g6 "rom the output o" 81dQ'lD&)r+ -help" show lines which contain 7sur"7, but not 7sur"ace7, then remove duplicates:
1dQ'lD&)r+ -help $ <rep s)r+ $ <rep -v s)r+ace $ s'rt $ )ni>

Some S%ecial 'iles 0in the home directory2


<cshrc : c-shell startup "ile (8csh run commands8) set aliases ad.ust the path set shell and environment variables <a+nirc : AFNI startup "ile

<s)marc : s)ma startup "ile <l'<in : commands run at the start o" a login shell (e6g6 a terminal window) <l'<')t : commands run be"ore e&iting a login shell <*ashrc : bash startup "ile (in case :ash your login shell)
19

You might also like