Using the find command in bash

Using the find command in bash

Find all files below the current directory that match “tecmint.txt”

find . -name tecmint.txt

Find all files below the current directory that start with “filena”

 find . -name "filena*"

Find files with a name, ignoring case

# find . -iname "FileName.txt"

Find directories in “/” with name “Dirname”

# find / -type d -name Dirname

Find all files of type “txt”

find . -type f -name "*.txt"

Exclude directories

find . -not \( -path ./miniconda3 -prune \) -name \*.py -print

External Resources

Find and Replace with find and sed

find . -type f -name "*.md" -print0 | xargs -0 sed -i 's/foo/bar/g'

Find text in a certain file type

Find links in markdown files ending in .mp4

find . -iname "*.md" -exec grep -l '.mp4)' {} \+ 

Find “video_source” keys in yaml files ending in .yaml

find . -iname "*.yaml" -exec grep -l 'video_source:' {} \+ 

excluding directories

find . -not \( -path ./miniconda3 -prune \) -name \*.py -print  -exec grep -l 'html' {} \+ 

excluding multiple directories

find . \( -path ./.config -prune -o -path ./miniconda3 -prune -o -path ./.local -prune \) -o -iname "*.md" -exec grep -l 'esptool' {} \+ 

alternate:

find . -iname "*.py" ! -path './miniconda*' ! -path './.config*'  ! -path './apps*'  ! -path './.vscode*' ! -path '*zenbook-backup*' -exec grep -l 'import network' {} \+ 

tips from here and here

find with maximum depth

from here

find . -iname "*.docx" -maxdepth 2

Ignore “permission denied” message

find . -iname “50_Lines_of” 2> >(grep -v ‘Permission denied’ >&2)

or simpler:

find . -iname “50_lines_of” 2>&1 | grep -v “Permission denied”