Skip to content
On this page

Midtern Review

Commands

Viewing Files

CommandDescription
cat [-n] [filename]Display a file on screen
less [filename]Display a file as a book (you can scroll back & forth)
head [-n count] [filename]Display the first n lines
tail [-n count] [filename]Display the last n lines
`paste [filename-]`

Managing Files and Directories

CommandDescription
ls [-lrt]list files and directories in a directory
cd [directory]change current directory
pwdprint working directory
cp [src] [dst]copy files
mv [src] [dst]move files
rm [-rf]remove files
mkdircreate directory
rmdirdelete directory (must be empty)
tar [-xcvf]create (or extract) on archive file
ln -screate a symbolic link
chmod [ugo+-rwx]change file permissions

File Analysis

CommandDescription
diff [-qy] [--color]Compare files
wc [-cwl] [filename]Word count. [#lines][#words][#characters][filename]
sort (-gk)Sort the lines of a file
uniq (-c)Delete repeated lines (leave unique lines)
fgrep [-inovw] [--color]Fixed string search
grep [-inovw] [--color]Regular expression search
egrep [-inovw] [--color]Extended regular expression search

Other Basic Commands

CommandDescription
echo [string]print the string, use -n to prevent newline
shiftremove $1 from $*, and then renumber
exitexit a script, returning the specified value
find . -namesearch for a file recursively
historylist the history of commands you typed
whichidentify the location of an executable
mandisplay the manual page for a command
`![numberprefix]`

More Advanced Commands

xargsAdd the pipe input to the argument list
exprCalculate an expression from arguments
tr (-dc)Replace (translate) or delete characteers
cut [-fcd] [--complement] [filename]cut charaters(-c) or fields(-f) from each input line
sed [-nf]Stream editor (Actions will be on the test: p=syz)

C-shell

Commands

  • if () cmd
  • if () then
    • else if () then
    • else
    • endif
  • if (-z/e file)
  • switch ()
  • while ()
  • foreach ($*)
  • $#argv
  • $argv[$#argv]
  • set X = $<
  • set X = word
  • set X = $3:q
  • set T
  • unset T
  • @ X = $2 + $Y

Variables

  • User created variables
    • $myvar, $file1, etc.
      • This also include array definition and usage based on subscript range
  • Keyword shell variables
    • $PATH, $prompt, $HOME, etc.
      • These have special meaning to the shell
  • Positional parameters
    • $1, $2, etc.
      • You will need to use shift if there are more than 9
  • Special parameters
    • $* - All arguments as a single string
    • $# - The number of command-line arguments
    • $#X - The number of elements in array X
    • $< - A line typed from keyboard (or redirected from a file)
    • $? - The exit status of the last command
    • $?X-Test to see if variable X exists

Symbols

SymbolDescription
. , .. , ~Current directory  /  Parent directory  /  Home directory
/Subdirectory separator in a path name
? , *Match one character  /  Match any number of characters
[], [^]Match one character from a set  /  not from a set
cmd<fileTake standard input from a file
cmd>fileRedirect standard output to a file
cmd>>fileRedirect standard output to the end of a file
cmd>& fileSend standard error messages also to file
`cmdcmd`
cmd ; cmdRun the 1st command and then run the 2nd
cmd && cmdRun the 2nd command only if the 1st fails
`cmd
(cmd; cmd; ...)Run command(s) in a subshell
cmd `cmd`Command substitution as an argument to another command
", ', \Quoting characters to control symbol substitution
$?, $?Vexit status of last command, existence check for variable V
$#, $#VNumber of: arguments to a script, elements in an array V
$*, $num, $VAccess the value(s) of: all arguments, an argument, a variable

Regular Expression

Wild card v.s. regex

  • A wild card pattern:
    • ls [a-e]*
      • This lists all files beginning with one of the first 5 letters
  • A regular expression pattern:
    • grep '[a-e]*' file
      • This matches all lines with 0 or more elements of the first 5 letters
        • For example, abcdebaceda
        • But the empty string is also a match (because 0 is allowed)
  • An extended regular expression pattern:
    • egrep '[a-e]*' file
      • This matches the same lines as the above grep did
      • But the matches would be different for egrep '[a-e]+' file
  • A simple list:
    • tr -d '[a-e]*' < file
      • This deleted every instance of any of the first 5 letters. But it also deletes the [, ], and * symbols
      • You see that? You don’t use [ and ] to enclose the lists for tr.
In regexIn csh wilcardMeaning
\\Same
[][]Same?
.?Same but different symbols
**Different

Regex symbols

SymbolDescriptionExample
^caret, as the first symbol of a regex, requires the expression to match the front of a line.line begins with 'A': ^A
$dollar sign, as the last symbol of a regex, requires the expression to match the end of a line.line ends with 'Z': Z$
\backslash, turns off special meaning for the next character.match to a literal '$': \$
[]brackets, matches to any one of the enclosed characters.match to any vowel: [aeiou]
.period, matches to any 1 character.a 1-character line: ^.$

Special Symbols Inside Brackets

SymbolDescriptionExample
-hyphen, inside [], matches to a range.a digit: [0-9]
^caret, as the first symbol inside [], matches any one character except those enclosed in the []not a letter: [^a-zA-Z]

The Position of The Caret

If the caret was not placed as the first symbol inside [], for example, [ab^cd], then it just represents a literal `^'.

Normal regex only

ExpressionDescription
\{x\}Matches x repetitions of the preceding regex.
\{x,y\}Matches x to y repetitions of the preceding regex.
\{,x\}Matches if the number of repetitions of the preceding regex x.
\{x,\}Matches if the number of repetitions of the preceding regex x.
\>The preceding regex must end at the end of a word.
\<The preceding regex must end at the start of a word.
\(...\)Define a group for a sub-portion of the regex. A group can be used before * or \{...\}, which match the repetition of the entire group.
\1, \2, etc.Backreference. Identify a rematch to the earlier pattern. We'll see the detail below.

Extended regex only

SymbolDescription
?Makes the preceding expression optional, i.e. \{0,1\}.
+Requires the preceding expression to occur at least once, i.e. \{1,\}.
``
()Can be used to change the associatibity of `

Regex vs. eregex

ExpressionIn grepIn egrep
`abcdef`the string `abc
`(a$)(b(cd)e)`
ab+cthe string ab+cabc or abbc, or abbbc, etc.
\([ab]\)\1aa or bb(a)1 or (b)1
a\{2an error, closing } not founda{2
\<awords begin with a<a (in standard egrep)