Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [155]

By Root 961 0
it means exactly that many days old. If negative, it means less than that absolute value. With a plus sign, it means more than that value. A common idiom is find . -mtime -7 to find files modified in the last week.

* * *

Warning


It is regrettable that find does not allow the number to have a fractional part or a units suffix: we've often wanted to specify units of years, months, weeks, hours, minutes, or seconds with these options. GNU find provides the -amin, -cmin, and -mmin options which take values in minutes, but units suffixes on the original timestamp selection options would have been more general.

* * *

A related option, -newer filename, selects only files modified more recently than the specified file. If you need finer granularity than a day, you can create an empty file with touch -t date_time timestampfile, and then use that file with the -newer option. If you want to find files older than that file, negate the selector: ! -newer timestampfile.

The find command selector options can be combined: all must match for the action to be taken. They can be interspersed with the -a (AND) option if you wish. There is also a -o (OR) option that specifies that at least one selector of the surrounding pair must match. Here are two simple examples of the use of these Boolean operators:

$ find . -size +0 -a -size -10

Find nonempty files smaller than 10 blocks (5120 bytes)

...

$ find . -size 0 -o -atime +365

Find files that are empty or unread in the past year

...

The -a and -o operators, together with the grouping options \( and \), can be used to create complex Boolean selectors. You'll rarely need them, and when you do, you'll find them complex enough that you'll hide them in a script once they are debugged, and then just use that script happily ever after.

A simple find script

So far, we have used find just to produce lists of files matching particular selection requirements, possibly feeding them into a simple pipeline. Now let's look at a slightly more complex example. In Section 3.2.7.1, we presented a simple sed script to (begin to) convert HTML to XHTML:

$ cat $HOME/html2xhtml.sed

Show sed commands for converting HTML to XHTML

s/

/

/g

s/

/

/g

...

s:

::g

s:::g

...

s/<[Hh][Tt][Mm][Ll]>//g

s:::g

s:<[Bb][Rr]>:
:g

...

Such a script can automate a large part of the task of converting from HTML to XHTML, the standardized XML-based version of HTML. Combining sed with find and a simple loop accomplishes the task in just a few lines of code:

cd top level web site directory

find . -name '*.html' -type f | Find all HTML files

while read file Read filename into variable

do

echo $file Print progress

mv $file $file.save Save a backup copy

sed -f $HOME/html2xhtml.sed < $file.save > $file Make the change

done

A complex find script

In this section, we develop a real working example of find's virtuosity.[8] It is a shell script named filesdirectories that some of our local users with large home-directory trees run nightly via the crontab system (see Section 13.6.4) to create several lists of files and directories, grouped by the number of days within which they have been changed. This helps remind them of their recent activities, and provides a much faster way to search their trees for particular files by searching a single list file rather than the filesystem itself.

filesdirectories requires GNU find for access to the -fprint option, which permits multiple output files to be created in one pass through the directory tree, producing a tenfold speedup for this script over a version that used multiple invocations of the original Unix find.

The script begins with the usual security features: specify the - option in the #! line (see Section 2.4):

#! /bin/sh -

set the IFS variable to newline-space-tab:

IFS='

'

and set the PATH variable to ensure that GNU find is found first:

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

export PATH

It then checks for the expected

Return Main Page Previous Page Next Page

®Online Book Reader