Tutorial 2
In this tutorial, we are going to learn some basic commands of Unix. We have two Unix servers to login:
cs1.utdallas.edu
cs2.utdallas.edu
To login to any of these servers remotely, we can use any of the following two programs:
For detail instructions on login, please refer back to Tutorial 1.
Some useful commands:
The commands we will see in this tutorial are:
lp
od
1. Locating commands:
1.1 The PATH:
The shell maintains a variable named PATH in its own environment. PATH is set to a list of colon-delimited directories. We can view it by evaluating the $-prefixed variable with echo command:
{cs2:~} echo $PATH
/usr/local/bin:/usr/bin:/usr/ccs/bin:/usr/ucb:/usr/openwin/bin:/usr/dt/bin:/opt/bin:/usr/xpg4/bin:
There are eight directories in the list. When we enter a command, the shell looks in each of these directories to locate the file with that name.
1.2 Three commands to get the clues:
There are three commands that provide clues to the location of another command.
The which command tells us the directory that contains the command, and displays its pathname or alias:
{cs2:~} which date
Querying terminal...
Terminal recognized as vt100 (Generic VT100)
/usr/bin/date
The whereis command essentially does the same thing, but it locates the binary, source and manual page files for a command:
{cs2:~} whereis date
date: /usr/bin/date /usr/man/man1/date.1
The "whereis" command looks up a larger list than "which" command which just searches the PATH.
The command "type" can give us more clues about the command. Sometimes it may be the case that there is a file which has the same name as a command but when we execute the command the shell uses its built-in function. This is the case with "echo".
{cs2:~} type echo
echo is a shell builtin
But in case of "ls", we see that the shell is using a file names ls:
{cs2:~} type ls
ls is hashed (/usr/bin/ls)
Thus "type" indicates how each name operand would be interpreted if used as a command.
The "type" utility indicates how each name operand would be interpreted if used as a command. "type" identifies the operand as a shell built-in, function, alias, hashed command, or keyword. More specifically, a "shell built-in" is a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute. A "hashed command" is an external command which is identified by the environment variable PATH.
2. Machine's name and operating system by uname:
The uname command displays the name of the operating system:
{cs2:~} uname
SunOS
In this example, the operating system is Sun Solaris. If you want to see the version of the operating system use "uname -r":
{cs2:~} uname -r
5.9
"uname -n" tells you the name of the machine:
{cs2:~} uname -n
cs2
3. Viewing help of a command by man:
To get help about a command, UNIX provides an extremely powerful tool, the "man" command. Suppose we want to view the help about "ls" command, we have to type "man ls":
{cs2:~} man ls
The page displayed will be like this:
To scroll down the page, we can press 'j'. To scroll up, we have to press 'k'. To quite, we have to press 'q'.
3.1 Further help with man -k and man -f
If we use the '-k' option with man command, it searches the NAME section of all man pages that contain the keyword. For example, if we want to know more about the uname command, we have to use:
{cs2:~} man -k uname
setuname setuname (1m) - change machine information
uname uname (1) - print name of current system
uname uname (2) - get name of current operating system
uuname uucp (1c) - UNIX-to-UNIX system copy
/usr/openwin/share/man/windex: No such file or directory
/usr/dt/man/windex: No such file or directory
The '-f' option simply displays a one-line header from the NAME section:
{cs2:~} man -f uname
uname uname (1) - print name of current system
uname uname (2) - get name of current operating system
/usr/openwin/share/man/windex: No such file or directory
/usr/dt/man/windex: No such file or directory
The "whatis" command emulates the "man -f":
{cs2:~} whatis cron
cron cron (1m) - clock daemon
4. printf alternative to echo:
"printf" works similarly like the printf funtion of C programming language. The difference with echo is that, we always have to use the escape sequence "\n" in order to move the cursor to next line. Here are two examples of printf:
{cs2:~} printf "Hello world\n"
Hello world
{cs2:~} printf "The current shell is %s\n" $SHELL
The current shell is /bin/bash
In the last example, "%s" is used as the placeholder for the value of $SHELL.
5. Recording the session with script command:
If you want to record all the commands you have typed in a session, you have to use the "script" command. Once, this command is invoked, it saves all the keystrokes in a file in the file typescript.
{cs2:~} script
Script started, file is typescript
sh-2.05$
After your recording is over, you can terminate the session with "exit":
sh-2.05$ exit
exit
Script done, file is typescript
To view the file, use "cat":
{cs2:~} cat typescript
Script started on Wed Aug 30 19:21:29 2006
sh-2.05$ pwd
/net/core/export/home/cs/001/m/mfh062000
sh-2.05$ exit
exit
script done on Wed Aug 30 19:22:26 2006
To view other logged in users, type "who":
{cs2:~} who
axm011500 pts/2
Aug 30 17:29 (dyna094218:0.0)
axm011500 pts/3
Aug 30 17:29 (dyna094218:0.0)
mfh062000 pts/4
Aug 30 18:04 (utdnat097205.utdallas.edu)
axm011500 dtremote Aug 30 17:29
(dyna094218:0)
The list shows the users. The first column contains the username, the second one tells in which terminal they logged on. The third column shows the login time. The last column shows the hostname from where the user logged in. To know the user who invoked the "who" command, use "who am i":
{cs2:~} who am i
mfh062000 /dev/pts/4 Aug 30 19:28
7. Printing a File
The printing system in Unix requires a user to spool (line up) a job along with others in a print queue. A separate program monitors this print queue and then picks up each job in turn for printing. The spooling facility in System V is provided by the lp (line printing) and 'cancel' commands. For example:
{cs2:~} lp -d csmfp3702 infile
request id is csmfp3702-776 (1 file(s))
lp notifies the request-id that can be later accessed with other commands. We can notify the user with the -m (mail) option after the file has been printed. Multiple copies can also be printed by -n option:
{cs2:~} lp -d csmfp3702 -n3 -m infile Print three copies and mail user a message
To view the print queue we can use the lpstat (line printer status) command. To cancel a job we can use 'cancel' command with the request-id of the job:
{cs2:~} cancel csmfp3702-776
8. Viewing Nonprintable Characters
Binary files contain nonprinting characters. To view them we have to use od (octal dump) command. When used with the -bc options, the output is quite readable:
{cs2:~} od -bc /bin/ls | more
0000000 177 105 114 106 001 002 001 000 000 000 000 000 000 000 000 000
177 E L F 001 002 001 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000020 000 002 000 002 000 000 000 001 000 001 017 000 000 000 000 064
\0 002 \0 002 \0 \0 \0 001 \0 001 017 \0 \0 \0 \0 4
Here we are viewing the /bin/ls binary file and we used 'more' command as the file is too large to fit in one screen. Each line displays 16 bytes of data in octal, preceded by the offset in the file of the first byte in the line. The first character has the ASCII octal value 177 and the next three comprise the string ELF. All C executables have the same first four characters. The second line shows the text representation of each character wherever possible.
The tutorial ends here!