Computer Command Shortcuts
HistoryThe C shell can recall a list of previous commands. Type:
to obtain a numbered list of previous commands. To select the nth one, type:
To repeat the last command, type !!. To repeat the command before the last, type !-2. One may also recall commands using the first characters from those commands. To repeat the last command starting with the character v, type !v. To repeat the last command that started with more, type !more.
Every C Shell command can be abbreviated by using the alias command. After typing
machine_name> alias h history
machine_name> alias ll ls -l
h is equivalent to history and ll is equivalent to ls -l.
In the Math Department we use an enhanced C shell, called the TC shell. This allows another means of repeating commands. You may press the arrow keys (to the right of the Shift and Ctrl keys on your keyboard) to get previous or next commands. For example, to see the last command you entered, just press the up-arrow key once. To see the command you entered the time before last, press the up-arrow twice. If you now choose to go back down the the last command, press the down-arrow.
Another powerful feature of the C shell is the ability to redirect output. Ordinarily, the output from a given command goes to the screen, but there are various ways to send it elsewhere. If you want the output from a command to go into a file, you may use the > symbol. For example, the command
machine_name> cat myfile > yourfile
has the same function as cp myfile yourfile. By itself, the command cat myfile would cause the contents of myfile to be listed on the screen. Here the > symbol tells UNIX to write the output (the listing) on a file called yourfile. If yourfile already exists, its contents are destroyed. If it does not exist, it is created.
Frequently you will not want to destroy an existing file, but will want to append the contents of another file on the end of it. For this, the >> symbol is used. The command
machine_name> cat myfile >> yourfile
appends the contents of myfile to the end of the file yourfile. If yourfile does not exist, it is created. It is also possible to redirect input using the < symbol, but this will not be discussed further here.
Another useful redirection symbol is the pipe ( | ). This tells UNIX to use the output of the command to the left of the | symbol as the input for the command to the right of the | symbol. For example, suppose that the directory papers contains so many files that their names no longer fit on one screen. To view all of the names, pipe the output from the ls command through the more command, as follows.
machine_name> ls papers | more
Background and Foreground
Finally, in UNIX you may run jobs in the background. Normally, commands accept input from and send output to the screen. A background job is more or less detached from the screen, so that the screen is free for you to type other commands in. This is equivalent to running a job in batch mode in VMS. You could run a program such as Mosaic in the background while reading your e-mail, for example. One may halt execution of a job at any time, and change its status from foreground to background, or vice versa. The way to halt execution of a job that is running in the foreground is to hold down the <ctrl> key, and press 'Z'. Doing this stops whatever job you are doing at the time. To restart the job in the foreground, type fg and hit the <Enter> key. To restart the job in the background, type bg, and hit the <Enter> key. You may also use fg to bring a background job to the foreground at any time.
One may start a job in the background immediately. To do so, simply type the command to start the program, followed by an ampersand ( & ). For example, to run Mosaic in the background while leaving your screen available for other things, type
machine_name> mosaic &
ProcessesTo check on the status of background jobs, use the ps (process status) command. To kill a background job, use the kill command (surprisingly transparent nomenclature for UNIX, isn't it?).
The following commands check on the process status of a program called myprog, and then kill it (This might occur if it's taking too long to execute - you might think that it is in an infinite loop).
PID TT STAT TIME COMMAND
5185 p1 I 0:00 -csh
6120 p1 I 0:59 myprog
The only commands entered from the keyboard were the first and last lines. The lines in between are the output from the ps command. The -9 flag on the kill command allows it to kill almost any job. You cannot kill someone elses job. Never kill the csh command, since that represents your current session.
If you try to log out, but the computer won't let you, saying that "there are stopped jobs", all you should have to do is type fg, and then kill the program that is stopped. After that, you should be able to log out. There are a number of other ways to use this feature to advantage.
Back to the on line help page