Lecture #9 – Utilities

 

 

 

Note:  the command in #5 is executed before any expansion and as a separate command line.

 

Note:  Variable assignment occurs before pathname expansion.

 

 

wc [-c] [-w][-l] [filenames]

 

counts the number of lines, words and characters

 

without any filenames, wc reads input from stdin.

 

Note:  the following two commands produce slightly different output.

 

$ wc –l cars

    14 cars

$ cat cars | wc –l

    14

 

 

$ A=test

$ readonly A

$ A=new

-bash: A: readonly variableq

 

readonly [filenames]

 

readonly without any parameters displays a list of readonly variables.

 

 

 

 

umask <permission mask>

 

Each bit TURNS OFF the permission for all files created after being set.

 

umask 000 – Leaves all bits unchanged, files are created with 666 permissions.

 

umask 066 – Turns off permissions for groups and others on new files.

 

umask 022 – Turns off Write permission for groups and others on new files.

 

 

 

            find <dir> options

 

Very flexible utility for finding files in a directory tree.

 

Many options, see page 600 for more info.

 

List all files starting in the current directory and working down, named “a.out”.

 

find . –name a.out –print

 

List all file under /usr/bin modified less than 300 days ago.

 

find /usr/bin –mtime -300 –print

 

Normally, options are combined as a AND, but can be change to an OR with the “-o” option.

 

find . –name a.out –print –o –name core –print

 

Find can also execute a command on each file it finds with the –exec option.

            The command must end with a “;” and {} is replaced with the filename.

           

find /tmp –mtime 3 –exec ls –l \{\}\ ;                 list all temp file older than 3 days.

 

 


 

 

Stores and retrieves files in an archive format. 

 

ALSO, useful for copying an entire directory and all subdirectories.

 

cpio –o             reads stdin for a list of filenames, and combines these files into a single archive that is written to stdout.

cpio –i              reads a file from stdin that it previously created with cpio –o.  cpio will then selectively extract files from the archive.

cpio –p             read stdin for a list of filenames, and passes these files to a directory given as an argument to the command.

 

ls *.c | cpio –o > source.archive            <-- creates a backup of C source files

 

cpio –i < source.archive                        <--  restores the archive

 

find cs390 –print | cpio –p uah  <--  copies directory cs390 to uah/cs390

 

 

 

The ‘set’ command takes the following parameters and assigns them to $1, $2, $3, etc.

 

Example:

 

$ wc carsort3

      31     121    719 carsort3

$ set `wc carsort3`

$ echo $1

31

$ echo $2

121

$ echo $3

719