Section 4.5. Finding Files: find


[Page 119 (continued)]

4.5. Finding Files: find

The find utility can do much more than simply locate a named file; it can perform actions on a set of files that satisfy specific conditions. For example, you can use find to erase all of the files belonging to a user tim that haven't been modified for 3 days. Figure 4-11 gives a formal description.

Figure 4-11. Description of the find command.

Utility: find pathList expression

The find utility recursively descends through pathList and applies expression to every file. The syntax of expression is described below, together with some examples of find.


Figure 4-12 is a table that describes the syntax of expression.

Figure 4-12. find expressions.

[Page 120]

Expression

Value/action

-name pattern

True if the file's name matches pattern, which may include the shell metacharacters *, [, ], and ?.

-perm oct

True if the octal description of the file's permission flags is exactly equal to oct.

-type ch

True if the type of the file is ch (b = block, c = char, etc.).

-user userId

True if the owner of the file is userId.

-group groupId

True if the group of the file is groupId.

-atime count

True if the file has been accessed within count days.

-mtime count

True if the contents of the file have been modified within count days.

-ctime count

True if the contents of the file have been modified within count days or if any of its attributes have been altered.

-exec command

True if the exit code from executing command is 0. command must be terminated by an escaped semicolon (\;). If you specify {} as a command-line argument, it is replaced by the name of the current file.

-print

Prints out the name of the current file and returns true.

-ls

Displays the current file's attributes (equivalent of ls -dils) and returns true.

-cpio device

Writes the current file in cpio format to device and returns true.

!expression

Returns the logical negation of expression.

expr1 [-a] expr2

Short-circuiting and; if expr1 is false, it returns false and expr2 is not executed. If expr1 is true, it returns the value of expr2.

expr1 -o expr2

Short-circuiting or; if expr1 is true, it returns true. If expr1 is false, it returns the value of expr2.



[Page 120]

Here are some examples of find in action:

$ find . -name '*.c' -print   ...print c source files in the        ...current directory or any of        ...its subdirectories. ./proj/fall.89/play.c ./proj/fall.89/referee.c ./proj/fall.89/player.c ./rock/guess.c ./rock/play.c ./rock/player.c ./rock/referee.c $ find . -mtime 14 -ls         ...ls modified files        ...during the last 14 days. 286580 16 -rw-r--r--  1 glass  cs 14151 May  1 16:58 ./stty.txt 286377  4 -rw-r--r--  1 glass  cs    48 May  1 14:02 ./file.doc 284428  4 -rw-r--r--  1 glass  cs    10 May  1 14:02 ./rain.doc 287331 16 -rw-r--r--  1 glass  cs 14855 May  1 16:58 ./tset.txt 288646 48 -rw-r--r--  1 glass  cs 47794 May  2 10:56 ./mail.txt $ find . -name '*.bak' -ls -exec rm {} \;                       ...ls and then remove all files                       ...that end with ".bak". 
[Page 121]
285451 4 -rw-r--r-- 1 glass cs 9 May 16 12:01 ./a.bak 282849 4 -rw-r--r-- 1 glass cs 9 May 16 12:01 ./b.bak 284438 16 -rw-r--r-- 1 glass cs 15630 Jan 26 00:14 ./s6/g.bak 284427 20 -rw-r--r-- 1 glass cs 18481 Jan 26 12:59 ./s6/g2.bak $ find . \( -name '*.c' -o -name '*.txt' \) -print ...print the names of all files that ...end in ".c" or ".txt". ./proj/fall.89/play.c ./proj/fall.89/referee.c ./proj/fall.89/player.c ./rock/guess.c ./rock/play.c ./rock/player.c ./rock/referee.c ./stty.txt ./tset.txt ./mail.txt $ _





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net