Command Line Editing, Alias, and Jobs

Working With Unix
[History |Rerunning Previous Commands |Editing Commands |Alias |Jobs |Processes |Kill |Exercises ]

History

The history command displays the last several commands that you have entered with a number associated with it. This allows you to know the number for rerunning past commands.

larry% history | tail
   237  16:58   ls
   238  16:59   grep the mp.and.the.holy.grail.txt | sort | more
   239  17:05   ls
   240  17:06   cat mp* | wc -l
   241  8:39    pwd
   242  8:52    history
   243  8:53    history | head
   244  8:53    history | head | tail
   245  8:53    history
   246  8:53    history | tail
larry% 
I have set my history list fairly long, so it is often necessary to pipe it trough tail.

Rerunning Previous Commands

You can rerun the last command by typing a "!" and the number of the command from history. For example, if I wanted to rerun the ls command, number 239, I could:

larry% !239
ls
a.out                           lunch
breakfast                       mp.and.the.holy.grail.txt
hi
larry% 
UNIX first displays the command that it is going to run, the runs the command.

Instead of using the process number, you can also use the first few characters of the command.

larry% !l
ls
a.out                           lunch
breakfast                       mp.and.the.holy.grail.txt
hi
larry%
If you only want to run the last command to ran again, you can just type "!!" instead of history, and then finding and typing the number.

larry% !!
ls
a.out                           lunch
breakfast                       mp.and.the.holy.grail.txt
hi
larry% 
You can also use this any of these as part of a command. For example, if you decided to cat a file, and it turned out to be very long, you could rerun it, and pipe it through more.

larry% cat mp.and.the.holy.grail.txt
 
"The Script, of The Soundtrack, of The Movie, of the Python Presentation, of
...
         (1:  The previous writer of the parenthesis has been sacked.)
                                (2:  THE END)
larry% !! | more
cat mp.and.the.holy.grail.txt | more
...

Editing Commands

You can edit the previous command line by using two of the "^" characters. The syntax for using this is:

^pattern^replacement pattern
This replaces the first occurrence of the pattern in the last command with the replacement pattern, and then runs the new command. For example, if you were trying to read the first two lines of the output returned from cating a file, but you were typing fast, and you didn't type the space or the "-" between the "head" and the "2".

larry% cat mp.and.the.holy.grail.txt | head2
head2: Command not found.
larry% ^head2^head -2
cat mp.and.the.holy.grail.txt | head -2
 
"The Script, of The Soundtrack, of The Movie, of the Python Presentation, of
A "^2^ -2" would have done the same thing. This can be a really useful thing if you are a messy typer like me.

Aliasing

Another command to make life easier in UNIX, is the alias command. The alias command allows you to set one command to run something else, or something more complicated. For example, if you want to run ls -l every time instead of just ls you could alias ls to be ls -l:

larry% ls
a.out                           lunch
breakfast                       mp.and.the.holy.grail.txt
hi
larry% alias ls ls -l
larry% ls
total 87
-rwxr-xr--  1 schallee    24576 Jul 13 09:54 a.out
-rw-rw-rw-  1 schallee       87 Jul 13 09:38 breakfast
-rw-r--r--  1 schallee       23 Jul 11 15:51 hi
-rw-r--r--  1 schallee       87 Jul 13 10:27 lunch
-rw-r--r--  1 schallee    60531 Jul 11 15:51 mp.and.the.holy.grail.txt
larry% 
Or, if you were an ex-DOS user, you could alias dir to be ls.

larry% dir
dir: Command not found.
larry% alias dir ls
larry% dir
a.out                           lunch
breakfast                       mp.and.the.holy.grail.txt
hi
larry% 

Jobs

UNIX is a multi-tasking system which means you can run more than one command at once. Doing this is called job handling. Before we can get into doing this, an understanding of the foreground and back ground is necessary.

The foreground is what you see. It is what actually is appearing on the screen. If you run a process, and you actually see the output, or have to wait for it to run, you are running the process in the foreground. Everything we have done so far has been in the foreground.

The background, is what you don't see. A process can be run in the back ground, and not inter fear with anything you are doing in the foreground. Background processes should have their output redirected so they do not try to display anything to the screen.

You can run a process, or a job, in the back ground, by appending the "&" character to the command line. An example of this would be if you wanted to sort a large file, but needed to get some other stuff done while it was running.

larry% sort -o mp.sorted.txt mp.and.the.holy.grail.txt &
[1] 11101
larry% ls
a.out                           lunch
breakfast                       mp.and.the.holy.grail.txt
hi                              mp.sorted.txt
[1]    Done                   sort -o mp.sorted.txt mp.and.the.holy.grail.txt
larry%
But what if you run a job, and you forget to run it in the background? You still can, but first you have to stop it. To stop a job that is running in the foreground, type "control-z," that is, hold down the control key and type "z." "^Z" is a more common notation for control characters. This should stop the process, and bring you back to a prompt.

larry% sort huge > sorted.huge
^Z
Suspended
larry% 
Now that you are back to the unix prompt, you can put it into the background and restart it there with the bg. The syntax is:

bg [ %n ]
where n is a job number. If the job number is not present, bg backgrounds the current job, which would be the one you just stopped.

larry% bg
[5]    sort huge > sorted.huge &
larry% 
The job is now running in the background.

But, what if you were running a process that was interactive, or you wanted to run in the foreground, but you had to stop it to do something else? Can you get it back? Yes. The counter part to bg is fg. fg works the same way as bg, except it takes stopped jobs and puts them in the foreground instead of the background.

larry% more mp.and.the.holy.grail.txt
 
"The Script, of The Soundtrack, of The Movie, of the Python Presentation, of
The Retelling of the Great Classic, of The Myth, of The Legend, of The
...
KING ARTHUR : Graham Chapman
PATSY : Terry Gilliam 
GUARD #1 : Michael Palin
--More--(1%)
Suspended
larry% ls
a.out                           lunch
breakfast                       mp.and.the.holy.grail.txt
hi                              sorted.huge
huge
larry% fg
more mp.and.the.holy.grail.txt
--More--(1%)
What if your doing lots of different things, and you don't know what processes are running, which ones are stopped, or any job numbers? This can be solved with the jobs command. It returns a list of the current jobs with their job number and their status.

larry% jobs
[1]  + Suspended              more mp.and.the.holy.grail.txt
[2]  - Running                sort -r sorted.huge > /dev/null
larry% 

Processes

Processes are a little different then jobs. They are all of the programs that you are running, including your shell. The way to get a list of the processes that are running is the ps command. It displays, the process id number, the terminal where the process is running, the status, the time, and the command being run. I am running X windows, so when I type in ps, I get a ton of processes:

larry% ps
  PID TT STAT  TIME COMMAND
 2481 co IW    0:00 /bin/sh /usr/openwin/bin/openwin
 2485 co IW    0:00 /usr/openwin/bin/xinit -- /usr/openwin/bin/xnews :0 -auth /ho
 2486 co S   290:16 /usr/openwin/bin/xnews :0 -auth /home/schallee/.xnews.larry:0
 2493 co IW    0:00 sh /usr/openwin/lib/Xinitrc
 2507 co S     2:16  (olwm)
 2508 co IW    0:00 olwmslave
 2810 co S    15:44 netscape -fg green -bg black
 7105 co S     3:37 xterm -sb -T shell -name shell -e tcsh
 7174 co S     0:35 xterm -sb -T shell -name shell -e tcsh
10669 co S     2:18 /usr/openwin/bin/mailtool
11007 co IW    0:01 xterm -sb -fg green -bg black -T lahs -name lahs -e rlogin la
 7175 p0 S     0:19 -csh (tcsh)
11469 p0 R     0:00 ps
 2528 p1 IW    0:16 -sh (csh)
 2818 p1 IW    0:35 xterm -bg black -fg green -T shell -name shell -e tcsh
 7995 p1 S     0:23 xterm -bg black -fg green -sb -T shell -name shell -e tcsh
10823 p1 IW    0:05 -csh (tcsh)
 7106 p2 IW    0:10 -csh (tcsh)
11029 p2 S     0:18 vi unix6.html
 2819 p3 IW    0:11 -csh (tcsh)
11008 p5 IW    0:00 rlogin lahs.losalamos.k12.nm.us
11009 p5 IW    0:00 rlogin lahs.losalamos.k12.nm.us
 7996 p6 IW    0:08 -csh (tcsh)
larry% 
ps also has a lot of options that let you see such things is all of the processes being run on the system.

Kill

If you want to stop a process completely, you can use the kill command. Kill is followed by options which are described in the man page, and then process id's or "%" job number.

larry% ps
  PID TT STAT  TIME COMMAND
 2481 co IW    0:00 /bin/sh /usr/openwin/bin/openwin
 2485 co IW    0:00 /usr/openwin/bin/xinit -- /usr/openwin/bin/xnews :0 -auth /ho
 2486 co S   290:43 /usr/openwin/bin/xnews :0 -auth /home/schallee/.xnews.larry:0
 2493 co IW    0:00 sh /usr/openwin/lib/Xinitrc
 2507 co I     2:17  (olwm)
 2508 co IW    0:00 olwmslave
 2810 co I    15:45 netscape -fg green -bg black
 7105 co I     3:42 xterm -sb -T shell -name shell -e tcsh
 7174 co S     0:37 xterm -sb -T shell -name shell -e tcsh
10669 co S     2:28 /usr/openwin/bin/mailtool
11007 co IW    0:01 xterm -sb -fg green -bg black -T lahs -name lahs -e rlogin la
 7175 p0 S     0:20 -csh (tcsh)
11485 p0 R     7:19 bad_guy
11493 p0 R     0:00 ps
 2528 p1 IW    0:16 -sh (csh)
 2818 p1 IW    0:35 xterm -bg black -fg green -T shell -name shell -e tcsh
 7995 p1 IW    0:23 xterm -bg black -fg green -sb -T shell -name shell -e tcsh
10823 p1 IW    0:05 -csh (tcsh)
 7106 p2 IW    0:10 -csh (tcsh)
11029 p2 I     0:21 vi unix6.html
 2819 p3 IW    0:11 -csh (tcsh)
11008 p5 IW    0:00 rlogin lahs.losalamos.k12.nm.us
11009 p5 IW    0:00 rlogin lahs.losalamos.k12.nm.us
 7996 p6 IW    0:08 -csh (tcsh)
larry% kill 11485
[2]    Terminated             bad_guy bad_guy
larry% 
Here is an example using job numbers:

larry% jobs
[1]    Running                bad_guy
larry% kill %1
larry% 
[1]    Terminated             bad_guy
larry% 

Exercises

  1. Get a list of the commands that you have been entering.
    larry% history
    
  2. Run it again with out typing it.
    larry% !!
    
  3. Run some other command again using its number.
    larry% !239
    
  4. Run some other command again by using the first few letters of the command.
    larry% !l
    
  5. Mistype a command, and change it using carrots.
    larry% ps | head2;
    head2: Command not found.
    larry% ^2^ -2
    
  6. Alias ls to be ls -l.
    larry% alias ls ls -l
    
  7. Run a job in the background.
    larry% sort huge > sorted.huge &
    
  8. Run a job, suspend it, and put it into the background.
    larry% sort huge > sorted.huge
    ^Z
    Suspended
    larry% bg
    
  9. Run a job, suspend it, and then bring it back into the foreground.
    larry% sort huge
    ^Z
    Suspended
    larry% fg
    
  10. Get a list of the jobs your running.
    larry% jobs
    
  11. List the processes you are running.
    larry% ps
    
  12. Start a process, suspend it, get the job number, and kill it. (Murderer)
    larry% innocent_victim
    ^Z
    Suspended
    larry% jobs
    [1]  + Suspended              innocent_victim
    larry% kill %1
     
    [1]    Terminated             innocent_victim
    larry% 
    

[Back |Contents |Next ]

Edward B. Schaller (schallee@earthlink.net)