1. Find and grep
1.1. find
The find tool, known from UNIX, is very powerful. This command not only allows you to search file names, it can also accept file size, date of last change and other file properties as criteria for a search.
The most common use is for finding file names:
find <path> -name <searchstring> |
This can be interpreted as "Look in all files and subdirectories contained in a given path, and print the names of the files containing the search string in their name" (not in their content).
find /etc -name "*.conf"
1.2. grep
grep is used for filtering input lines and returning certain patterns to the output.
grep "string" path/to/file |
grep "root" /etc/passwd
1.3. find and grep command together
find /etc -name "*.conf" -exec grep -Hns "conf" {} \;
Explanation
-H, --with-filename
Print the filename for each match
-n, --line-number
Prefix each line of output with the 1-based line number within its input file
-s, --no-messages
Suppress error messages about nonexistent or unreadable files.
This can be interpreted as - "Look for *.conf files and subdirectories contained in /etc, and if true exec grep -Hns conf in the given file" |
Very powerful in bash scripts create a file test.sh .bash script |
#!/bin/bash
STRING=$(find /etc -name "*.conf" -exec grep -Hns "conf" {} \;)
echo $STRING
exec it .bash script
chmod 700 test.sh
./test.sh