Unix Essentials
Featuring the Solaris 10 Operating System
Module 2
Finnbarr P. Murphy SCSA RHCSA RHCE
(fpm@fpmurphy.com)
Unix
• Unix is multi-user and multi-tasking operating
system.
• Multi-tasking: Multiple processes can run
concurrently.
• Example, different users can read mails, copy
files, and print all at once.
Unix
Processes
• Running Programs
User owned
System owned
Files
• Regular Files:
– Data
– Executables
• man man
• man –k, catman rebuilds whatis database
• Examples: man passwd, man –k passwd
Basic Commands
• pwd: working directory (/u0/users/2/kliu1).
• ls: list contents of directory
• mkdir : make directory
• rmdir : remove an empty directory
• rm –r : remove a directory with all the
contents
• cd : change directory, ~/ means your
home directory
• cp : copy command.
Basic Commands
• chmod : change mode of a
file/directory
• ls –l : list with details
– 9 permission bits: d r w x r w x r w x
– 3 categories: user/group/all
– Permissions: read/write/execute (r/w/x)
– mode= 644 means r w _ r_ _ r _ _
• first 3 bits for user
• next 3 bits for group
• last 3 bits for others
– Remember: RWX, 421, UGO
Basic Commands
• rm : remove files
e.g.: rm –fr directory/filename
• mv : change the name of a file
• Pipes: Connect the stdout of one command
with the stdin of another command
e.g.: ls -l | more or ls –l | less
Other Commands
• cat Echos file contents to the screen
• grep Searches a file for a string
(General Regular Expression Parser)
• more Echos a file a line at a time
• less Same as more but more features
• wc Counts the words in a file and more
• sort Sorts the contents of a file
Pipes and Redirection
The Unix “philosophy” involves a set of generic
tools which each do one thing well. Using these
tools together allows you to appreciate the
power of the Unix command.
In Unix everything (including hardware devices)
is treated as a File that can be read from and
written to.
Pipes
Program 1
stdout
Pipe Program 2
stdin
stdout
Pipe Program 3
stdin
Pipes
• Pipes connect stdout of one command
to stdin of another comand.
i.e.
• ls | less
• cat student_list | grep senior | sort
I/O Redirection
• I/O redirection allows the user to change
where input to a command or output from
a command goes to/comes from
• cat student_list > outfile (overwrite)
– cat student_list >> outfile (append)
• program outfile
Standard File Handles
All standard shells provide a facility to re-
map these three file handles to other
devices and files in addition to other
commands.
This allows one to use several simple Unix
commands to perform a complex task
Standard File Handles
Every Unix process automatically comes with
three file handles or descriptors
These are:
– Standard Input (stdin)
• Keyboard
– Standard Output (stdout)
• Display
– Standard Error (stderr)
• Display (unbuff)
Three Default Open Files
Program stdout Buffer
stdin stderr
Program & Process
• A program is an executable file that resides on
a file system (disk)
• A process is an executing instance of a
program
• A Unix process is identified by a unique non-
negative integer called the process ID
– PID
• Check process status using the “ps” command
Foreground & Background
• A program can run either as
– a foreground process or
– a background process
• Use the ampersand operator “&” to create a
background process
– $ back &
• Otherwise a foreground process is created
– $ back
Foreground & Background
• Only 1 foreground process for each session
– Multiple background processes.
• Where are background processes used?
– All system daemons
– long user processes, etc.
• printer-daemon process
• mailer-daemon process.
• Some processes are always running in background
Process Status
$ back &
[1] 16488 the process id assigned by system
$ ps
PID TTY TIME CMD
1973 pts/39 0:01 sh
16488 pts/39 0:00 back
Stop a Process
• Foreground processes can generally be stopped by pressing
CONTROL C (^C)
• Background processes can be stopped using the kill command
• Usage: kill SIGNAL
• kill -9 (-9 means no blocked)
Or kill
• If a foreground process is not stopping by ^C, you can open
another session and use the kill command
Archive Files
• tar (tape archive)
– tar cvf demo.tar create archive
– tar tvf demo.tar list contents of archive
– tar xvf demo.tar extract contents of archive
• Other archivers include:
– cpio copy in/copy out
– pax portable archiver extended
Compress/Uncompress Files
• gzip
– gzip demo.tar compress demo.tar demo.tar.gz
– gunzip demo.tar.gz uncompress demo.tar.gz -> demo.tar
• Other methods include:
– compress/uncompress
– zip
– bzip
– bzip2
Identification
• uname
– uname -a all system information
– uname –m machine information
– uname –p processor information
– uname –r release level
• id
• whoami
• who am i
Regular Expressions - grep
• The grep command searches its target for a
specific string. It is a very powerful tool.
• Regular expression are used in combination
with grep and other commands to do pattern
matching.
• * - allows selection of 0 or more characters
– Example: *.doc selects anything that end with
.doc
Regular Expressions
• ? – allows selection of any single character.
– ?J will select any occurrence of the letter J.
• . – following any character will match that character.
– X. will match the X followed by any character.
• ^ - matches the characters that follow it if they are at
the beginning of a line.
– ^J selects any line that begins with a J
• $ - matches the characters that follow it if they are at
the end of the line.
– J$ selects any line that ends with a J
Grep Options
• -b Prints the block number where the pattern was
found
• -c Prints the count of lines matching the pattern
• -i Matches upper or lower case
• -l Prints only the filename of those matching the
pattern.
• -n Prints the line number where the pattern was
found.
Grep Options
-v – suppresses lines that match the pattern. In
other words, search for everything BUT the
pattern
-w – search for the pattern as if it were a word
AWK
• Special-purpose language for line-oriented
pattern processing
– Aho, Weinberger and Kernighan
– Data driven tool
– More intuitive syntax than shell scripts
– Avoids read loop
• Search text files
– Lines that contain certain patterns
– Perform specified actions on those lines
– Keep processing until end of file(s) reached
AWK
• awk != nawk != gawk
• pattern {action}
• action =
– if (conditional) statement else statement
– while (conditional) statement
– break
– continue
– variable=expression
– print expression-list
AWK
• Add up first column, print sum and average
{s += $1 }
END {print “sum is”, s, “average is”, s/NR}
• Print all lines between start/stop words:
/start/,/stop/
• Print all lines whose first field differs from
previous one:
$1 != prev {print; prev = $1}
SED
• Stream Editor
– Line-oriented tool
• Text files
– Regular patterns (REGEX)
• Pattern matching and replacement
– No programming language
– Apply same change to one or more source files
• Does not modify the source (input) file
SED
• pattern a text add to output
• address s /regex/replacement/
• address d delete line
• delete lines 1-10: sed -e '1,10d‘
• delete comments: sed -e '/^#/d‘
• print only matching: sed -n -e '/regexp/p
• convert Unix to DOS: sed -e 's/$/\r/' myunix.txt >
mydos.txt
Other Commands
Every Unix distribution comes with hundreds of
other standard commands that can be used for
everything from viewing and searching files to
developing software.
Look through /bin, /usr/bin, and /usr/local/bin on
most systems and look at the man pages for the
programs in those directories for more
information.