For anyone who is working on Ubuntu/ on a linux platform, they might have initially found it difficult for navigation along directories. In this post, we shall see a few of the commands and their syntax that would help you understand and make your navigation easy.
This is the base pattern we would be following:
ππ’π§π /π©πππ‘/ππ¨/π¬πππ«π -ππ²π©π π -ππ±ππ π π«ππ© -π ‘π©πππππ«π§’ {} \;
– /path/to/start: The directory where you want your search to begin.
– -type f: This restricts the search to files only.
– -exec: This allows you to run another command on the files that find locates.
– grep -H ‘pattern’: The grep command searches within the file for the specified pattern. The -H option makes grep print the filename along with the matching line.
– {}: This is a placeholder that represents the current file that find is processing.
– \;: This ends the -exec command.
1) Search recursively but exclude certain directories:
find /path/to/start -type d \( -name “exclude_dir1″ -o -name exclude_dir2” \) -prune -o -type f -print | xargs grep -H ‘pattern’
2) Search with case-insensitive matching using grep:
find /path/to/start -type f -print | xargs grep -iH ‘pattern’
3) Search for multiple patterns using grep with the -e flag:
find /path/to/start -type f -print | xargs grep -H -e ‘pattern1’ -e ‘pattern2’
4) Find files that do NOT contain a certain pattern using grep -L:
find /path/to/start -type f -print | xargs grep -L ‘pattern’
5) Search for a pattern in files modified in the last N days using find with -mtime:
find /path/to/start -type f -mtime -N -print | xargs grep -H ‘pattern’
6) Using xargs for faster searching with grep in large file sets:
find /path/to/start -type f -name “*.php” -print | xargs grep -H ‘database’
7) Search for files based on size and then grep within those files:
find /path/to/start -type f -size +1M -print | xargs grep -H ‘pattern’
8) Search for a pattern within files accessed or modified recently:
find /path/to/start -type f -atime -2 -print | xargs grep -H ‘pattern’
9) Find and grep within specific file types:
find /path/to/start \( -name “*.txt” -o -name “*.md” \) -print | xargs grep -H ‘pattern’
10) Using grep with context to show lines before and after the match:
find /path/to/start -type f -print | xargs grep -H -A3 -B3 ‘pattern’
11) Searching for a pattern in all but certain file types:
find /path/to/start -type f ! -name “*.log” -print | xargs grep -H ‘pattern’
12) Search for a pattern within files, excluding certain patterns:
find /path/to/start -type f -print | xargs grep -H ‘pattern’ | grep -v ‘exclude_pattern’
13) Find files based on depth to avoid recursive searching and grep within those:
find /path/to/start -maxdepth 1 -type f -print | xargs grep -H ‘pattern’
14) Using zgrep to search within compressed files:
find /path/to/start -type f -name ‘*.gz’ -print | xargs zgrep ‘pattern’
15) Using grep to show only the filenames without the matching content:
find /path/to/start -type f -print | xargs grep -l ‘pattern’
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
An Article by: Yashwanth Naidu Tikkisetty
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
