CS246-F20-01-UnixShell
Lecture 1.10
• More UNIX commands,
– find
CS246
• find searches for files/dirs within a directory hierarchy,
according to various stated criteria
– Like a command-line version of MacOS Spotlight
• Usage:
• Options:
-name pattern restricts file names to globbing pattern
If dir-list omitted, search starting in current directory, “.”
If expr omitted, match all file names (same as –name “*”)
find [ dir-list ] [ expr ]
find
Square brackets (usually) means “optional”!
find
Example: Recursively find file/dir names matching pattern “t*”,
starting in current dir:
More options:
-type f | d select only normal files (f) or only dirs (d)
-maxdepth N recursively descend at most N directory levels
(‘0’ means current dir)
$ find -name “t*” # Important: Why quotes?
./test.cc
./testdata
./oldTests/test-y2k-dir
find
More options:
• logical not, and and or (precedence order)
• if no operator given, then “expr1 expr2” means
“expr1 -a expr2”
• \( expr \) can use parens to specify evaluation order
-not expr
expr1 -a expr2
expr1 -o expr2
• Recursively find only “normal” file names matching pattern
“t*” starting in current dir
$ find . -type f -name “t*”
test.cc
• Recursively find only file names in list (excluding hidden files)
to a max. depth of 3, matching patterns t* or *.C
$ find . -maxdepth 3 -a -type f -a \( -name “t*” -o –
name “*.C” \)
test.cc
q1.C
testdata/data.C
find examples
% ls
main.cc main.o zlurble/
% find . –type f
./main.cc
./main.o
./zlurble/bazz/balloon.cc
./zlurble/bazz/main.cc
./zlurble/kalumph.cc
./zlurble/readme.txt
% find . -name *.cc # Why do we get the second output line?
./main.cc
./zlurble/bazz/main.cc
% find . -name “*.cc”
./main.cc
./zlurble/bazz/balloon.cc
./zlurble/bazz/main.cc
./zlurble/kalumph.cc
End
CS246