Search directory for a word / text via the command line
By using grep we can quickly search through files for a specific string of text
To list all occurrences of a particular string / text in your Bash (or ZSH or Shell) command line, run:
searchdir
# Search current directory for files containing specified string (Usage: searchdir "Search Term")searchdir() {if [[ $# -eq 0 ]] ; thenecho -e "\e[0;31mPlease provide a string / search term\e[0m"elsegrep -rni "$@" .fi}
grep -ni
grep -ni "Text to search for" .
You can also search recursively in subdirectories by adding the r flag, for example:
searchdir
# Search current directory for files containing specified string (Usage: searchdir "Search Term")searchdir() {if [[ $# -eq 0 ]] ; thenecho -e "\e[0;31mPlease provide a string / search term\e[0m"elsegrep -rni "$@" .fi}
grep -rni
grep -rni "Text to search for" .
If you use this sort of thing a lot, you could always add this to your ~/.profile, ~/.bashrc, or ~/.zshrc in handy aliases like:
searchdir
# Search current directory for files containing specified string (Usage: searchdir "Search Term")searchdir() {if [[ $# -eq 0 ]] ; thenecho -e "\e[0;31mPlease provide a string / search term\e[0m"elsegrep -rni "$@" .fi}