find [path...] [expression]
Default path is the current directory
Default expression is -print
The argument that begins with ( ) - , or ! is taken to be the beginning of the expression
true), true or false ), all separated by operators.
options
Placed at the beginning of the expression.
-depth | Process files in directory before the directory itself. |
-prune | do not descend the current directory; true. With -depth, false; no effect.
|
| Descend at most levels of directories below the command line arguments. 0 i.e. only apply the tests and actions to the command line arguments.
|
-mindepth l | Do not apply any tests or actions at levels less than levels
1 i.e. process all files except the command line arguments.
|
-follow | DEreference symbolic links. Implies -noleaf.
|
-noleaf | Needed when searching non-Unix filesystems, such as CD-ROM or MS-DOS or AFS. Each directory on a Unix filesystem has at least 2 hard links: its name and its . entry. Its subdirectories each have a .. entry linked to that directory. |
‑xdev ,
‑mount | Don't descend directories on other filesystems. |
-daystart | Measure times from the beginning of today rather than from 24 hours ago. (see -amin, -atime, -cmin, -ctime, -mmin, and -mtime) |
-version
| |
-help
|
hh*60mm. hh=?
or
since
Example,
starting at the current directory, find files, larger than 6MB created recently
(i.e. within last 8 hours) ( if -type f is omitted ls will follow directories) Example:
Find, beginning in the parent/parent directory ,
( all
Field widths and precisions can be specified .
expr1
SEE ALSO
locate(1L), locatedb(5L),
updatedb(1L), xargs(1L)
bash -d, -ftests
+n for greater than, -n for less than.
-name pattern Filename matches pattern . The metacharacters
( *, ?, and []) do not match a . at the start of the base name.
(i.e. find . -name "*rc" does not find .bashrc).
To ignore a directory and the files under it, use -prune:
find . -path ./src/emacs -prune -o -print
-iname pattern case insensitive.
ex: "fo*" or "F??" matches Foo, FOO, foo, fOo, etc.
-regex pattern File name matches regular expression pattern.
This is a match on the whole path, not a search.
For example, to match a file named fubar3 use the regular expression bar. or b.*3 but not b.*r3
-iregex pattern case insensitive.
time related options
Numeric arguments can be specified as +n for greater than, -n for less than, ommitting the sign indicates equal =accessed (Not all systems update the time files are accessed see mount -o noatime )
-amin n accessed exactly n minutes ago.
-amin -5 for files accessed in last 5 mintes.
+4464 lists files not accesses in last 31 days.
-anewer file more recently than file was modified.
affected by -follow only if -follow comes before -anewer on the command line.
-atime n n*24 hours ago (days ago )
†.
status changed
-used n n days after its status was last changed.
-cmin n status was last changed n minutes ago.
an cut off the mode flags and owner.
/usr/bin/find . -type f -size +12000 -cmin -480 -exec /bin/ls -lg {} \; |cut -c20-
-cnewer file more recently than file was modified.
affected by -follow only if -follow comes before -cnewer on the command line.
-ctime n n*24 hours ago (days ago) †.
data modified
-mmin n data was last modified n minutes ago.
Use -mmin -10 for less than 10 minutes ago.
-mtime n n*24 hours ago (days ago)†.
Remove files (and direcrories) not modified in the last 2 years
find . -not -mtime -730 -exec rm -r {} \;
-newer file more recently than file.
affected by -follow only if -follow comes before -newer on the command line.
only files, newer than DBtables.cgi and
do an ls supressing the group and cutting off the mode-flags and the owner.
find ../.. -type f -newer DBtables.cgi -exec /bin/ls -lg {} \; |cut -c21-
-size ±nk more|less than n kilobytes,
nb blocks(default),
ncharacters , or 2-byte words nw.
-empty Well, if the file is empty
-path pattern name matches shell pattern pattern.
The metacharacters do not treat
/ or . specially; so, for example,
find . -path ./sr*sc
will print an entry for a directory called ./src/misc .
To ignore a whole directory tree, use -prune rather
than checking every file in the tree.
For example, to skip the directory src/emacs and all files and directories under it,
and print the names of the other files found:
find . -path ./src/emacs -prune -o -print
-ipath pattern match is case insensitive.
-perm mode File's permission bits are exactly mode (octal or symbolic).
Symbolic modes use mode 0 as a point of departure.
-perm -mode All of the permission bits mode are set for the file.
-perm +mode Any ...
-false
-true -fstype type filesystem of type type, including ufs, 4.2, 4.3, nfs, tmp, mfs, S51K, S52K.
Use -printf with the %F directive to see the types of your filesystems.
-type t block (buffered) special, character (unbuffered) special,
directory, named pipe (FIFO),
regular file,
symbolic link, socket , door (Solaris)
-xtype t For symbolic links:
In other words, for symbolic links, -follow has not been given, true if the file is
a link to a file of type t;
-follow has been given, true if t is l.
-xtype checks the
type of the file that -type does not check.
-uid n owned by user n.
-user uname owned by user uname (numeric user ID allowed).
-nouser owner no longer exists, i.e. No /etc/passwd record
-gid n numeric group ID is n.
-group gname belongs to group gname (numeric group ID allowed).
-nogroup group no longer exists.
-inum n has inode number n.
-links n has n links.
-lname pattern symbolic link whose contents match pattern .
The metacharacters do not treat / or . specially.
-ilname pattern case insensitive.
actions
-print is the default action on all files for which expression is true.
-exec command [ args ] \;
†
Where
{}
occurs in args,
find replaces
them
with filename found
and then executes command in the starting directory.
Evalutes to true if command returns 0 status.
one way to execute multiple commands is to create an sh file containting the commands
If
find . -iname "*html" -exec grep -H astrix {} \; |cut -c1-$COLUMNS
./docs/_html-notes.html:<b><a href=...<img src=astrix.gif title="
./docs/partimage.1.html:<code>partimage <span class=astrix title=" bgzip compress ">-z2</span>
find returns a directory;
a command that recursively processes the directory, like ls ,
many more actions will occur then expected.
perhaps -type f will help as then only files will be found.
du with --max-depth=1 will not recurse.
/cygdrive/c/Documents and Settings
$ find . -name Favorites -exec /usr/bin/du {} --max-depth=1 \;
68 ./Administrator/Favorites/Links
16 ./Administrator/Favorites/hiking
28 ./Administrator/Favorites/searches
172 ./Administrator/Favorites/someday
4 ./Administrator/Favorites/watching
36 ./Administrator/Favorites/finance
697 ./Administrator/Favorites
0 ./All Users/Favorites
-ok command ; Like -exec but asks first.
If the response does not start with y or Y, the command is not executed, and returns false.
/usr/bin/find . -iname "*" -ok rm {} \;
-ls list current file in ls -ilds format with
on STDOUT.
size in K†.
; true
serial b premissions l user group size modify date filename
number
1153 4 -rw-r--r-- 1 rea reag 739 May 23 20:08 part.c
973 4 drwx------ 6 rea reag 4096 Apr 26 19:57 tmp
1158 0 lrwxrwxrwx 1 root sys 11 Dec 19 21:09 www -> pub
-fls file true; like -ls but write to file like -fprint.prints return true)
-print output the full file name followed by a newline.
-print0 followed by a null . Used if file name might contain \n
-fprint ofile output the full file name to ofile.
-fprint0 ofile
-printf "specs" output specs
-fprintf ofile "specs"
Does not add a newline at the end of the string (so use \n at the end of the specs).
Interprets \ escapes and % as directives.
escapes:
A \a alert bell. \b Backspace. (repositions cursor over previous char)
\c Stop and flush the output.
\f Form feed. \n Newline. \r Carriage return.
\t Horizontal tab. \v Vertical tab.
\\ backslash. %% percent sign.
\NNN The character whose ASCII code is NNN (octal).
\ followed by any other character is treated as an ordinary character, both are printed.
directives:
%a access time
%Ak format specified by k
either fc† or a directive for the C strftime function.
"%AT"
13:36:18
%c last status change time.%Ck formated
%t modification time %Tk formated
formatting codes fc
Time fields:
r 12-hour hh:mm:ss [AP]M T 24-hour hh:mm:ss
X H:M:S Z time zone
H hour 00..23 h 01..12 k 0..23 l 1..12
M minute 00..59 p AM or PM
S seconds 00..61
@ seconds since Jan. 1, 1970, 00:00 GMT.
Date fields:
D mm/dd/yy x mm/dd/yy
c Sat Nov 04 12:02:33 EST 1989
a Sun..Sat A Sunday..Saturday
w day of week 0..6
b Jan..Dec h B January..December(variable length
m month 01..12 d day of month 01..31
y 00..99 Y 1970...
j day of year 001..366
U week number of year with Sunday as first day of week 00..53
W week number of year with Monday
%kv size in 1K , %b in 512-byte blocks , %s in bytes.
%d File's depth in the directory tree; 0 i.e. the file is a command line argument.
%h Leading directories of file's name (all but the last element).
%f name with any leading directories removed (only the last element).
%p name.
%P File's name with the name of the command line argument under which it was found removed.
%H Command line argument under which file was found.
%F Type of the filesystem the file is on; this value can be used for -fstype.
%g group name, or numeric group ID if the group has no name.
%G numeric group ID.
%i inode number
%l Object of symbolic link (empty string if not a symbolic link).
%m permission bits (in octal).
%n Number of hard links to file.
%u owner, or numeric user ID if the user has no name. %U numeric user ID.
A % character followed by any other character is discarded (the other character is printed).
OPERATORS
Default operator is -and .
Listed in order of decreasing precedence:
\(†( expr \)†
\!† expr true if expr is false.
-not
expr1 -and expr2
expr2 is not evaluated if expr1 is false. ,
expr1 expr2 And (implied); expr1 -a expr2
-o expr2 Or; expr2 is not evaluated if expr1 is true.
expr1 -orv expr2
expr1 , expr2 List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2.
if [ -d $DirOrFile ] ; then echo "$DirOrFile is a directory"; fi