Unix Mod2
Unix Mod2
About Shells
● The are many shells. Some common ones, in historical order, are:
❍ sh: the original Bourne shell.
■ C shell like syntax with many bug fixes and Tenex-like extensions.
■ Proprietary.
❍ Other directories are referred to by name, and their names are separated by /
■ Why MicroSoft later chose to use a backslash \ instead of the already established / as a directory
separator is a mystery.
● If a path refers to a directory it can end in /
● An absolute path starts at the root of the directory hierarchy, and names directories under it
● In the root directory is a directory called bin, which contains a file called ls:
/bin/ls
● The following example will run the ls command, by specifying the absolute path to it:
[you@faraday you]$ /bin/ls
● The lc command is in the directory /usr/local/bin/ so to run it with an absolute path you type:
[you@faraday you]$ /usr/local/bin/lc
● We can use lc to list files in a specific directory by specifying the absolute path:
[you@faraday you]$ lc /usr/local/lib
● On many UNIX/Linux systems, the user's home directory is /home/you/ where you is typically your login
name.
● As mentioned in the previous Module, the shell maintains an idea of your present working directory –
the directory in which you are working.
● By default, the bash prompt displays the name of the directory that is your present working directory as the last
item.
● Commands like ls use the current directory if none is specified
● Use the pwd command to see what your current directory is:
[you@faraday you]$ pwd
/home/you
[you@faraday you]$ _
● To delete a directory that is not empty you may change into the directory and remove all its contents, change
back to the parent of the directory, and then use rmdir
● Another way to delete a non-empty directory is to the use rm -r
[you@faraday you]$ rm -r NonEmptyDirecotory
[you@faraday you]$ _
Relative Paths
● For example, the following sets of directory changes both end up in the same directory:
[you@faraday you]$ cd /
[you@faraday /]$ cd usr
[you@faraday usr]$ cd share
[you@faraday share]$ cd doc
[you@faraday doc]$ _
● Relative paths specify files inside directories in the same way as absolute ones. The following two sets of
commands both execute the program lc in the directory /usr/local/bin
[you@faraday you]$ /usr/local/bin/lc
● Every directory contains two special subdirectories which help making relative paths
● The directory .. points to the parent directory, so to list the files in the directory which contains the current
directory, use ls ..
❍ For example, if we start from the directory /home/you
● There can also be files and directories whose name begins with a period, such as .profile
● These "dot files" and "dot directories" are hidden in the sense that neither ls or lc show them by default.
❍ Nothing else about these files and directories are special.
● You can see the existence of the dot files and directories, along with all the other files and directories, by giving
options to ls and lc:
❍ ls -a shows all files and directories
❍ lc -h shows the hidden files and directories along with all the others
● You can get the paths to other users’ home directories using ˜. This for a user alice:
[you@faraday some_directory]$ cat ~alice/somefile
[you@faraday some_directory]$ cd ~
● If a filename contains spaces, or characters which are interpreted by the shell (e.g., *, ˜), put single quotes around
them:
[you@faraday some_directory]$ rm ’Strawberry Fields.mp3’
[you@faraday some_directory]$ cat ’* important notes.txt *’
● The shell expands the wildcard, and passes the full list of files to the program, in the above example to ls
● Just using * on its own will expand to all the files and directories in the current directory. Thus the following
command removes all the files in the present working directory!
[you@faraday some_directory]$ rm *
[you@faraday some_directory]$ _
The following, then, will remove all files and sub-directories of the present working directory!
[you@faraday some_directory]$ rm -r *
[you@faraday some_directory]$ _
❍ When the shell expands file and directory names with *, it does not include names that begin with a
period (.). A moment's reflection will convince you that this is a good thing.
Looking at Files
● You can use an editor to look at the contents of an ASCII text file.
● You can use cat to output a small ASCII file to your screen. A long file, such as Faraday's password file
/etc/passwd, will scroll off the top of your terminal window leaving only the last 24 lines or so visible.
● From an X-terminal, you may have scroll bars on the terminal window so you may see what has scrolled off the
top of the screen. They may be moved using the middle button of the mouse.
❍ For a two-button mouse, holding down both buttons simultaneously simulates the middle button.
● You can look at the first few lines of a file with head
[you@faraday some_directory]$ head /etc/passwd
● You may see the last few lines of a file with tail
[you@faraday some_directory]$ tail /etc/passwd
❍ The -f option to tail follows the file as it grows. So to watch accesses to Faraday's web server, you
can follow the log file /usr/local/apache/logs/access_log:
[you@faraday some_directory]$ tail -f
/usr/local/apache/logs/access_log
● You may use the pagers more or less to scroll through a file a screenful at a time.
[you@faraday some_directory]$ more /etc/passwd
● You can view lines in a file that contain a particular string with grep
[you@faraday some_directory]$ grep 'harrison' /etc/passwd
❍ The origins of the name grep are the original UNIX editor ed. It stands for "global regular expression
print." We will discuss regular expressions in more detail later.
● For a binary non-text file, the program od will show its contents in octal or hexidecimal
● For a binary non-text file, the program strings extracts and displays the ASCII strings that are in the file.
■ The next section discusses the meaning of the word "pipes" in the previous sentence.
● The man pages are divided into sections. For example, Section 1 is for user commands, Section 5 is for file
formats, Section 8 is for administration commands. If the man program is given two arguments, the first is the
section and the second is the name of the man page. It searches the sections in numerical order.
❍ To get the man page for the passwd command that is used to change your password the following are
equivalent:
[you@faraday some_directory]$ man passwd
❍ To get the man page that describes the format of the password file itself use:
[you@faraday some_directory]$ man 5 passwd
● The system maintains a database of keywords in the man pages. You can search for man pages for a given
keyword with:
❍ There is lots of documentation available on-line. Most people automatically pipe the output to a pager:
[you@faraday some_directory]$ apropos keyword | more
This "pipe" operation using the vertical bar | is the topic of the next section.
■
❍ Although the man command exists on virtually every UNIX/Linux flavor, info usually appears only
with relatively modern Linux distributions.
■ On modern Linux distributions there are beginning to appear GNU programs which do not have
page.
❍ The info system includes nodes, menus, etc.
■ The top of screen always shows the current node, the next and previous nodes if any, and the
parent "up" node if any. Simple navigation of nodes can be done with:
Keystroke Action
space Scroll to next screenful like more and less
n Go to next node
p Previous node
u Up to parent node
b Back to the beginning of the current node
q Quit info
h Invoke an on-line tutorial on info
■ If there is a next node after the current one, pressing the space bar at the end of a node
will take you to the next node. Otherwise it starts over from the beginning of the current
node.
❍ For simple cases, such as looking at the info page for mkdir, info is almost identical to the man
command.
❍ Menus are identified by an asterisk *:
* Menu:
■ Press the m key. A line will open at the bottom of the screen in which you can type the name of
1.
Plumbing
● The output of a utility is directed to a stream called stdout, for "standard output."
● For many utilities, if not given an argument it reads a stream called stdin (for "standard input") for its input
❍ When we created files using cat in Module 1, we were reading the keyboard to supply the characters
❍ You can then enter the next command and press Enter:
[you@faraday some_directory]$ cat /etc/passwd |
> wc
2786 3199 161149
[you@faraday some_directory]$ _
❍ This syntax was also used to create a file using cat in Module 1.
● The shell can direct stdin from a file with the less-than symbol <
[you@faraday some_directory]$ wc -l < /etc/passwd
2786
[you@faraday some_directory]$ _
❍ It also accepts a -H flag which adds a label to each column of its output:
❍ The options can be combined. All of the versions below are equivalent:
[you@faraday some_directory]$ who -i -H
■ For well-behaved programs, the space between the option and its argument is optional:
[you@faraday some_directory]$ last -n10
■ In the early days of UNIX, the space between the option and its argument was not allowed.
Sadly, some of this ancient code survives today.
❍ The -R flag suppresses the listing of the machine the user logged in from:
[you@faraday some_directory]$ last -R
● Command line options with and without arguments can be combined. All the following forms are identical:
[you@faraday some_directory]$ last -R -n10
● In the early days of UNIX, command options were a single letter preceded by a single hyphen. Now options can
be multiple letter strings preceded by two hyphens.
❍ The who command accepts a long option --version which prints its version number.
❍ The date command accepts a -d flag for manipulating the date shown. The following command shows
■Note that in the long version an equal sign = connects the option with its argument.
● The man pages for options follow several different conventions.
❍ In older man pages which document only options that begin with a single hyphen, a fragment of the man
■ The ellipsis … indicate parts of the man page not duplicated above.
■ Entires surrounded by square brackets, [ … ] are options.
■ Commands that require arguments will list them not surrounded by square brackets.
■ The last command also accepts the options -a, -d, -i, and -o. The full man page document
these too.
■ Some programs have on-line help which looks similar to the synopsis.
DESCRIPTION
Print selected parts of lines from each FILE to standard
output.
…
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
-f, --fields=LIST
output only these fields; also print any line that
contains no delimiter character, unless the -s
option is specified
■ Since the long form options --delimiter and --fields require arguments, it is
understood that the short form equivalents do also. In the old form these would have been
described as:
-d DELIM Use DELIM instead of TAB for field delimiter
-f LIST Output only these fields; also print any
line that contains no delimiter character,
unless the -s option is specified
Exercise 1
● From your home directory execute pwd and note the name of the parent of your home directory. Below we shall
call that directory /home but your system may be configured differently.
● Use cd to go to the root directory. View its contents.
● Change to the /home directory and view its contents. Typically these are the home directories of all users except
for the master user root.
❍ On Faraday, the student users' home directories are located elsewhere.
● Use grep on /etc/passwd to extract the line corresponding to one of the directories of /home. See if you can
find the part of the line that specifies that user's home directory.
● Use the man page on the format of the password file to determine the meaning of the other fields in the line that
you extracted.
● Change to your home directory and confirm that that is where you are.
● Use the hostname command, with no options, to print the hostname of the machine you are using.
● Use man to display some documentation on the hostname command. Find out how to make it print the aliases
for the machine. You will need to scroll down the manpage to the ‘Options’ section.
Exercise 2
● Create a text file in your home directory called shakespear, containing the following text:
● Rename it to sonnet-18.txt.
● Make a new directory in your home directory, called poetry.
● Move the poem file into the new directory.
● The * wildcard on its own is expanded by the shell to a list of all the files in the current directory. Use the echo
command to see the result (but make sure you are in a directory with a few files or directories first)
● Use quoting to make echo print out an actual * symbol.
● In the poetry directory create another file, sonnet-29.txt:
● Use the cat command to display both of the poems, using a wildcard.
● Create a directory in your home directory named tmp
● Copy all the files in the poetry directory into the tmp one.
● Finally, use the rm command to delete the poetry directory and the poems in it.
Exercise 3
● Use grep on /etc/passwd to see your entry. Identify your login shell.
● Combine grep and wc to count the number of users who use the same shell as you for their login shell.
● Learn about the -v flag to grep from its man page. Use it to count the number of users who do not use your
login shell.
● Use the utility sort to list the users of your login shell, sorted by their login.
● Use cut to display a sorted list of these users that displays only their login.
❍ Now you can begin to appreciate the power of having a command history available to avoid typing the
/etc/passwd. Thus you will need a -d flag to set the delimiter. You will also need a -f flag to tell it
which field to cut out and send on to stdout.