find [path …] [options] [expression …] |
This is the man page for the
GNU version 4.5.11
BSD version
| |
path
is the current directory
options
: affect overall operation (rather than the processing of a specific file) . Example:(-xdev
)
expression
may be composed of:
operators
return true
or false
-ls -exec rm {} \;
)expression
is -print
-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 lev of directories below the path … . 0 only apply the tests and actions to path … |
+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 /usr/bin/find . -name 2011-08-\*
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-iname pattern |
-print
is the default action on all files for which expression
is true.
If
Find recently backup files ;
( all
\†
\†
expr1 -and expr2
expr2
SEE ALSO
locate(1L), locatedb(5L),
updatedb(1L), xargs(1L)
bash -d, -f
on Mac OS X , search the spotlight database instead of the directory (Much faster)
use locate to search the database of files that existed when updatedb was last run.
Current GNU version is 2014-07-19 html man Release findutils-4.5.14. maintained by James Youngman jay@gnu.org
-exec command [{}]
[ args
] \;
†
If
{}
occurs in args
, find
replaces it
with the filename found and then executes command
in the starting directory.
Evalutes to true if command
returns 0 status.
To execute multiple commands create an sh file containting the commands. find -iname "*html" -exec grep -H astrix {} \; |cut -c1-$COLUMNS
./astartix.html:<table><tr><th>monospace really looks like a star not an astrix.
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 temp.\*† -ok rm {} \;
-ls
list filenames in ls -ilds
format with on STDOUT
.
size in K
†.
Returns 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
use grep to supress chache files
Use sed to format -ls
to be like ls -log
;
\* escape the star with a slash to prevent shell expansion
/usr/bin/find . -iname 2011-08-\* -mmin -52 -ls |\
grep -vi cache |\
sed -f find.sed †
sort -n|tail
find -ls -size +5k | sed -f find.sed †
-fls file
true; like -ls but write to file like -fprint
.print
s return true)
-print
output the full file name followed by a newline.
-print0
filename followed by a null .
Useful if file name might contain \n or spaces or other special characters .
Useful with xargs
Specifiy Field widths and precisions.
-fprint
ofile output the full file name to ofile.
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.
-fprint0
ofile
-printf
† "specs" output specs
-fprintf
ofile "specs"
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 output
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
Directives
%k
size in 1K , %b
in 512-byte blocks , %s
in bytes.
%d
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 output).
OPERATORS
All options
return true
tests
and actions
return true
or false
.
Listed in order of decreasing precedence:
Default operator is -and
.
( expr
\†)
! expr
false if expr
is true.
-not
is not evaluated if expr1
is false.
expr1 -a expr2
expr1 expr2
And (implied)
expr1 , expr2
List; both expr1
and expr2
are always evaluated.
expr1 -o expr2
Or;
expr2
is not evaluated if expr1
is true.
expr1 -or expr2
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
† find.sed
s/^[[:digit:]]\{1,9\} \{1,9\}[[:digit:]]\{1,6\} .......... \{1,5\}[[:digit:]]\{1,4\} [[:alnum:]]\{1,8\} \{1,10\}[[:alnum:]]\{1,8\} \{1,10\}//