Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [157]

By Root 971 0
\

-a -mtime -14 -fprint $TMP/DIRECTORIES.last14.$$ \

-a -mtime -7 -fprint $TMP/DIRECTORIES.last07.$$ \

-a -mtime -2 -fprint $TMP/DIRECTORIES.last02.$$ \

-a -mtime -1 -fprint $TMP/DIRECTORIES.last01.$$

When the find command finishes, its preliminary reports are available in the temporary files, but they have not yet been sorted. The script then finishes the job with a loop over the report files:

for i in FILES.all FILES.last31 FILES.last14 FILES.last07 \

FILES.last02 FILES.last01 DIRECTORIES.all \

DIRECTORIES.last31 DIRECTORIES.last14 \

DIRECTORIES.last07 DIRECTORIES.last02 DIRECTORIES.last01

do

sed replaces the prefix ./ in each report line with the user-specified directory name so that the output files contain full, rather than relative, pathnames:

sed -e "s=^[.]/=$WD/=" -e "s=^[.]$=$WD=" $TMP/$i.$$ |

sort orders the results from sed into a temporary file named by the input filename suffixed with .tmp:

LC_ALL=C sort > $TMP/$i.$$.tmp

Setting LC_ALL to C produces the traditional Unix sort order that we have long been used to, and avoids surprise and confusion when more modern locales are set. Using the traditional order is particularly helpful in our diverse environments because our systems differ in their default locales.

The cmp command silently checks whether the report file differs from that of a previous run, and if so, replaces the old one:

cmp -s $TMP/$i.$$.tmp $i || mv $TMP/$i.$$.tmp $i

Otherwise, the temporary file is left for cleanup by the trap handler.

The final statement of the script completes the loop over the report files:

done

At runtime, the script terminates via the EXIT trap set earlier.

The complete filesdirectories script is collected in Example 10-1. Its structure should be clear enough that you can easily modify it to add other report files, such as for files and directories modified in the last quarter, half year, and year. By changing the sign of the -mtime values, you can get reports of files that have not been recently modified, which might be helpful in tracking down obsolete files.

Example 10-1. A complex shell script for find

#! /bin/sh -

# Find all files and directories, and groups of

# recently modified ones, in a directory tree, creating

# lists in FILES.* and DIRECTORIES.* at top level.

#

# Usage:

# filesdirectories directory

IFS='

'

PATH=/usr/local/bin:/bin:/usr/bin # need GNU find for -fprint option

export PATH

if [ $# -ne 1 ]

then

echo "Usage: $0 directory" >&2

exit 1

fi

umask 077 # ensure file privacy

TMP=${TMPDIR:-/tmp} # allow alternate temporary directory

TMPFILES="

$TMP/DIRECTORIES.all.$$ $TMP/DIRECTORIES.all.$$.tmp

$TMP/DIRECTORIES.last01.$$ $TMP/DIRECTORIES.last01.$$.tmp

$TMP/DIRECTORIES.last02.$$ $TMP/DIRECTORIES.last02.$$.tmp

$TMP/DIRECTORIES.last07.$$ $TMP/DIRECTORIES.last07.$$.tmp

$TMP/DIRECTORIES.last14.$$ $TMP/DIRECTORIES.last14.$$.tmp

$TMP/DIRECTORIES.last31.$$ $TMP/DIRECTORIES.last31.$$.tmp

$TMP/FILES.all.$$ $TMP/FILES.all.$$.tmp

$TMP/FILES.last01.$$ $TMP/FILES.last01.$$.tmp

$TMP/FILES.last02.$$ $TMP/FILES.last02.$$.tmp

$TMP/FILES.last07.$$ $TMP/FILES.last07.$$.tmp

$TMP/FILES.last14.$$ $TMP/FILES.last14.$$.tmp

$TMP/FILES.last31.$$ $TMP/FILES.last31.$$.tmp

"

WD=$1

cd $WD || exit 1

trap 'exit 1' HUP INT PIPE QUIT TERM

trap 'rm -f $TMPFILES' EXIT

find . \

-name DIRECTORIES.all -true \

-o -name 'DIRECTORIES.last[0-9][0-9]' -true \

-o -name FILES.all -true \

-o -name 'FILES.last[0-9][0-9]' -true \

-o -type f -fprint $TMP/FILES.all.$$ \

-a -mtime -31 -fprint $TMP/FILES.last31.$$ \

-a -mtime -14 -fprint $TMP/FILES.last14.$$ \

-a -mtime -7 -fprint $TMP/FILES.last07.$$ \

-a -mtime -2 -fprint $TMP/FILES.last02.$$ \

-a -mtime -1 -fprint $TMP/FILES.last01.$$ \

-o -type d -fprint $TMP/DIRECTORIES.all.$$ \

-a -mtime -31 -fprint $TMP/DIRECTORIES.last31.$$ \

-a -mtime -14 -fprint $TMP/DIRECTORIES.last14.$$ \

-a -mtime -7 -fprint $TMP/DIRECTORIES.last07.$$ \

-a -mtime -2 -fprint $TMP/DIRECTORIES.last02.$$ \

-a -mtime -1 -fprint $TMP/DIRECTORIES.last01.$$

for i in FILES.all FILES.last31

Return Main Page Previous Page Next Page

®Online Book Reader