Updated: 03 July 2023
Search for the whole word 96730
grep "\<96730\>" app.log
Jersey & Guernsey, United Kingdom
Updated: 03 July 2023
Search for the whole word 96730
grep "\<96730\>" app.log
Updated: 18 May 2023
Search for several patterns at once
grep 'pattern1\|pattern2\|pattern3' log.txt
Updated: 16 July 2023
Find tell
in trans.sql and show 3 lines of context surrounding the result
grep -C 3 tell trans.sql
Show 3 context lines before and 2 lines after matches
grep -B 3 -A 2 bar index.php
Show --only-matching
parts of the matching line
grep -o foo index.php
Updated: 13 January 2023
Find all occurrences of foo in directories and files recursively, from the current directory
grep -irn --exclude-dir=dir --text foo * -i: case insensitive -r: recursive but don't follow symlinks -n: show line number --text: treat binary files as text
Interpret PATTERNS as --fixed-strings
, not regular expressions
grep -F './dir/dir2' file.txt
Updated: 20 November 2023
Find directories with either foo or bar in their name
find . -type d \( -iname '*foo*' -or -iname '*bar*' \) | sort
Find files modified within the last 6hrs i.e. 0.25 of 1 day
find /home/chris -type f -mtime -0.25
Find files where name (case insensitive) matches ‘*.exe’
find ~/.nuget/packages -iname "*.exe"
Find directories where name starts with ‘site’
find . -type d -name 'site*'
Find any of the named files (risky, test first)
find this-dir -type f \( -name "foo" -o -name "bar" -o -name "log" \)
Find uses ;
(or +
) to terminate the -exec
command. Therefore \;
must be used because ;
is one of the operators (also &&
or ||
) which separates shell commands.
Find files by text in the file.
find . -type f -name "*.java" -exec grep -il string {} \;
-i
: ignore case
-l
: show filenames, not the match.