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.
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
...
^pattern^replacement patternThis 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, ofA "^2^ -2" would have done the same thing. This can be a really useful thing if you are a messy typer like me.
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%
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 [ %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
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
larry% jobs [1] + Suspended more mp.and.the.holy.grail.txt [2] - Running sort -r sorted.huge > /dev/null larry%
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.
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%
larry% history
larry% !!
larry% !239
larry% !l
larry% ps | head2; head2: Command not found. larry% ^2^ -2
larry% alias ls ls -l
larry% sort huge > sorted.huge &
larry% sort huge > sorted.huge ^Z Suspended larry% bg
larry% sort huge ^Z Suspended larry% fg
larry% jobs
larry% ps
larry% innocent_victim ^Z Suspended larry% jobs [1] + Suspended innocent_victim larry% kill %1 [1] Terminated innocent_victim larry%