Skip to main content

find, search, grep

find

use cases:

  • find ./subfolder -name sample.txt - Search a file with specific name
  • find ./subfolder -name *.txt - Search a file with pattern
  • find ./GFG -name sample.txt -exec rm -i {} \; - find and delete a file with confirmation
  • find ./subfolder -empty - Search for empty files and directories
  • find ./subfolder -perm 664 - Search for file with entered permissions
  • find ./ -type f -name "*.txt" -exec grep 'some Phrase' {} \; - Search text within multiple files

more examples

find /path -name *.txt
find /path -type f -name test.txt
find /path -name failed*.* -type f
find /path -type f -not -name "*.html"
find / -name "file.txt" -size +4M
find /dev/ -type b -name "sda*"
find ./*file*

grep

grep [OPTION]... PATTERNS [FILE]...

  • options
    • -B <numb> - show numb lines before match
    • -A <numb> - show numb lines after match
    • -i - ignore case distinctions in patterns and data
    • -r, --recursive - like --directories=recurse
    • -v, --invert-match - To display only the lines that do not match a search pattern
    • --exclude-dir=<foldername> - exclude folder from search

examples

  • grep -ir --exclude-dir=vendor skeleton . - find all occurences of "skeleton" in the current working dir
  • grep -i "some string" path/**/files.log - search string in log files