22
Additional UNIX Commands

Additional UNIX Commands. 222 Lecture Overview Multiple commands and job control More useful UNIX utilities

Embed Size (px)

Citation preview

Page 1: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

Additional UNIX Commands

Page 2: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

222

Lecture Overview

Multiple commands and job control

More useful UNIX utilities

Page 3: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

333

Executing Multiple Commands

Several commands can be executed in a single line, by separating them with ';'s:

To run a set of commands in a sub-shell, enclose them within brackets:

set a = /home ; echo $a/demo

/home/demo

( set a = /home ; echo $a/demo )

Page 4: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

444

Executing Commands in a Sub-shell – Example

pwd

/home/demo

(set p = /etc; cd $p ; pwd ; ls -l passwd)

/etc-rw-r--r-- 1 root root 15692 Dec 3 23:12 passwd

pwd

/home/demo

echo $p

p: Undefined variable.

Page 5: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

555

The sleep Command

The sleep command just does nothing for the given number of seconds:

Can be useful for interactive scripts

We will use it to demonstrate job control

sleep number

Page 6: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

666

Job Control

A job is any command line that can be executed by the shell

Multiple jobs can be run concurrently The shell allows you to manage jobs:

Place a job in the background Move a job to the foreground Suspend a job Kill a job

Page 7: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

777

Running Jobs in the Background

Typing any command, followed by the '&' symbol, will cause the job to be run in the background The job number is printed out for reference

When a job is running in the background, you may continue to type new commands

A message is displayed when the job is complete

Page 8: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

888

Running Multiple Jobs

The jobs command will list all current job

sleep 10 &

[1] 3215

(sleep 5 ; pwd ; sleep 5) &

[2] 3216

jobs

[1] + Running sleep 10[2] - Running ( sleep 5; pwd; sleep 5 )

Page 9: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

999

Running Multiple Jobs

After 5 seconds, the following is printed:

After a few more seconds:

And finally:

/home/demo

[1] Done sleep 10

[2] Done ( sleep 5; pwd; sleep 5 )

Page 10: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

101010

Suspending or Killing the Foreground Job

Pressing Ctrl-Z while some job is running in the foreground will stop its execution The job stops, but is not dead The suspended job will show up in the jobs

output, with a status of suspended

Pressing Ctrl-C will completely terminate (or 'kill') the foreground job

The kill command will kill a specific job

Page 11: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

111111

Moving Jobs Between Background and Foreground

The bg command starts running the specified job in the background The job number should be preceded by a '%'

The fg command is used to move any background job to the foreground The job will start to run in the foreground,

whether it was running or suspended

Page 12: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

121212

Jobs – Example

We type the following two commands, and press Ctrl-Z immediately after each one:

sleep 10

Suspended

(sleep 5 ; pwd ; sleep 5)

Suspended

jobs

[1] + Suspended sleep 10[2] - Suspended ( sleep 5; pwd; sleep 5 )

Page 13: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

131313

Jobs – Example

Now, we start running the second job in the background:

To move the first job to the foreground:

bg %2

[2] ( sleep 5; pwd; sleep 5 ) &/home/demo

fg %1

Sleep 10

Page 14: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

141414

Lecture Overview

Multiple commands and job control

More useful UNIX utilities

Page 15: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

151515

More Useful Utilities

The UNIX operating system has many other commands

We will briefly review several additional ones now, but you are encouraged to investigate and learn: More commands Additional options and uses for the commands

that we have learned

Page 16: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

161616

Simple Loop – repeat

The repeat command executes any command multiple times

For example:

repeat count command

repeat 3 echo Hello

HelloHelloHello

Page 17: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

171717

Date and Time – date

The date command can be used to print the current date and time:

The format argument can be used to control the display of the current time

It may contain literal characters, or special sequences starting with '%'

date [options] [+format]

Page 18: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

181818

date – Examples

For example, '%A' is the weekday, '%Y' is the current year, etc.

date +%D

12/04/05

date +"%A, %B %d"

Sunday, December 04

date +"Date: %A, %B %d, %Y; Time: %l:%M"

Date: Sunday, December 04, 2005; Time: 4:26

Page 19: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

191919

Getting User Information

Several UNIX commands allow us to get information about other users currently logged-on to the system: who – Shows who is logged on w – Shows who is logged on, and also what they

are doing finger – Provides various information about

users currently logged-on

Page 20: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

202020

Splitting Data Streams

The tee command takes input from the standard input, and writes it both to the standard output, and to a list of files:

The following will list all files in the current directory into the file 'all.txt' , and also print a count of all '.c' files in the directory

tee [options] files

ls | tee all.txt | grep '.c' | wc -l

Page 21: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

212121

The find command

The find command searches for files whose name matches a pattern

The most common use of find:

Finds all files that match pattern in directory or in any of its sub-directories

find has many other options and uses

find directory -name pattern

Page 22: Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities

222222

find – Example

find can execute any command

One common use is to execute the grep command on all files:

This will traverse the current directory and all of its sub-directories, and search for the string 'Hello' in each simple file

find . -type f -exec grep -H "Hello" '{}' \;