Searches
find
find [<path>] [-maxdepth n] [-type [dfl]] [-name <file-name>, –regex “<regex>”]
[-mmin -<max-minutes>] [-mtime -<max-days>]
[-exec <command> [options] {} \;] [-print,-print0]
Options:
<path> : Path to search from (default: . ) (required on Solaris)
-maxdepth : Maximum depth to search (default: no limit)
-type (by default, searches for all types)
d: directory
f: file
l: link
-name : File name pattern (default: * )
-regex : Regular-expression file name pattern
-mmin : Files changed within the last n minutes
-mtime : Files changed within the last n days
-exec : Run the specified command for each file that's found.
"{}" is replaced with the name of the current file.
Only a single command can be run.
-print : Print the file name followed by a newline (this option is used by default)
-print0 : Print the file name followed by a null character
(for supporting file names with blanks spaces or newlines)
- Locates a file
# Search “data” directories for .txt files containing the text “hello”
find -type d -name data -exec find {} -type f -name "*.txt" \; | xargs -l1 grep -li 'hello'
# Replace "hi" with "bye" for all .html and .txt files in "data" directories
for ext in html txt; do
find -type d -name data -exec \
find {} -type f -name "*.$ext" \; | xargs -l1 \
perl -pi -e "s|/hi/bye|ig;"
done
# The following lines are equivalent (recursively list all files in the current directory)
find -type f -name "*"
find . -type f
find -type f
-regex
Has to match the entire relative-path file name, starting with ./- Regex characters that have to be escaped: ( ) |
- Regex characters not supported: { }
-exec
# Search .js files
find -name "*.js" -exec grep -il "text" {} \;
# Search a subset of .js files (excluding *_data.js)
find \( -name "*.js" -a ! -name "*_data.js" \) -exec grep -il "text" {} \;
# Search .php and .js files
find \( -name "*.php" -o -name "*.js" \) -exec grep -il "text" {} \;
# Search .php and a subset of .js files
find \( -name "*.php" -o \( -name "*.js" -a ! -name "*_data.js" \) \) -exec grep -il "text" {} \;
- Only one command can be run for each file
{}can be used more than once
for ... in
# Search .js files
for i in `find -name "*.js"`; do grep -il "text" $i; done
# Search .php and .js files
for i in `find \( -name "*.php" -o -name "*.js" \)`; do grep -il "text" $i; done
- Using find in combination with a for loop allows multiple commands to be run for each file
vi $(...)
# Edit a set of .php files
vi $(find -name "*.php" -exec grep -il "text" {} \;)
grep
grep [-<options>] <pattern> <file-name>
Options:
l: Print the file name (and nothing else) for each match
L: Print the file name (and nothing else) for each unsuccessful match
H: Print the file name (and the full line of text) for each match
n: Print the line number for each match
i: Case insensitive
w: Match whole words only
E: Extended regular expression
P: Perl regular expression
R, r: Recursive
I: Skip binary files
z: Treat the entire file as a single line
- Searches for a string within a file
- Matches patterns on a line-by-line basis
- Prints the entire line for any matches; no obvious way to only
display the matching portion of a line
# recursively search for "text" in all files
grep -r "text" *
# Identify where paths are added to the PATH variable
cd /etc
find -type f -exec grep -q PATH {} \; -print
# For each file containing the text "hello"
grep -lr 'hello' * | xargs -L1 ls
# For each *.txt file containing the text "hello"
find -type f -name "*.txt" -exec grep -l 'hello' {} \; | xargs -L1 ls