Have you ever spent large chunks of time, hopelessly trying to find a file or find which file contains a certain snippet of code? There are two linux commands FIND and GREP that can be very helpful in these cases, and also many other cases aswell. When it comes to locating data on your system, both provide a powerful and flexible way to do so. It is worthwhile spending time understanding how they work, and adding them to your essentials toolbox.
The commands are great for linux servers or virtual machines where you do not have access to a GUI. In Windows and MacOS off course you can simply use the built in file search tools for basic functionality, i.e. Spotlight search or Windows Desktop Search, and or code editors i.e. Xcode, Visual Studio Code, PyCharm to search within project directories. The Windows terminal also has a similar command to grep, 'findstr', see link in reference. Where ever possible, it is most advised to work locally using a text editor for code of any complexity, this gives you the freedom and ease of use to do find and replace, and search easily using a native interface.
We generally use FIND for searching filenames and use GREP to search content inside of files, the two can also be used together. Below, we'll introduce these commands and their basic usage, accompanied with some more advanced use cases for grep.
The basic command for finding a file follows the expression, 'find WHERE WHAT', or more specifically:
find [path] [expression]
The results are given as the paths to files that match the command. A common path used is *. In which case * is a wildcard, it matches everything in the current directory (except, by default, files/directories starting with a .). Note find, automatically searches recursively, where grep does not.
An example is:
find ./ -name '*jpg'
other useful arguments:
| -name | restricts your results to files that match the given expression. If you don't use -name will simply list all files recursively from the starting point folder. |
| -iname | case insensitive |
| -o | logical OR |
| -type | f to search for files. d for directories |
| -size | i.e. '+1G' |
| -mtime | modification time in days, - (less than), or + (more than). i.e. '-mtime -7' |
| -mmin | same as above, but in minutes |
For more advanced commands check out the manual pages or the links in the reference below.
The GREP command is used to search text within specific files or folders. It searches for a match of the given strings or words, within lines of the specified files and folders. The GREP is a very powerful command, especially when combined with regular expressions in the search text. It can be a useful tool for finding all occurrences of a search term in a selection of files, filtering a log file or stream, or as part of a script or chain of commands.
The basic command for searching files follow the expression, 'grep WHAT WHERE'. It is generally used to search a particular file, but can also be used recursively to search a directory. The grep results list both paths to the files, and the line that contains the matched string. Either of these can be suppressed using the command options -h, or -l respectively.
grep [option...] [patterns] [file...]
If you want to search multiple files in a directory and recursively through all subdirectories, the -r flag enables recursive searching through a directory tree. As mentioned above, find by default searches recursively, whereas grep does not.
The true power of grep lies in its ability to use regular expressions for complex pattern matching. Regex patterns are also supported by the -E option if you want to search for a set of strings rather than one literal.
You may also redirect output from a command to grep using a pipe:
tail -f /var/log/apache/error.log | grep 'some text'
Use cases for grep commands:
Search all files in current directory and sub-directories re-cursively
grep -r "string" .
Search all files in current directory only and skip errors
grep -d skip "Book" *
| -i | Ignore case distinctions on Linux and Unix |
| -w | Force PATTERN to match only whole words |
| -v | Select non-matching lines |
| -n | Print line number with output lines |
| -h | Suppress the Unix file name prefix on output |
| -r | Search directories recursively on Linux |
| -R | Just like -r but follow all symlinks |
| -l | Print only names of FILEs with selected lines |
| -c | Print only a count of selected lines per FILE |
| --color | Display matched pattern in colors |
https://opensource.com/article/18/4/how-use-find-linux
https://phoenixnap.com/kb/guide-linux-find-command - explains usage of FIND combined with GREP, and delete and execute arguments
https://www.linode.com/docs/tools-reference/tools/how-to-grep-for-text-in-files/
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
https://searchdatacenter.techtarget.com/tip/Using-grep-and-sed-to-find-and-replace - using grep and sed to do find and replace
https://www.itechtics.com/using-findstr-windows/