How to Find a Specific Word in a File on Linux
How to Find a Specific Word in a File on Linux.
By default, most search tools look at file names, not file contents. However, the most famous GNU search program,
grep
, will look inside files with the correct flags. Here we will show you how you can find specific word(s) in a file on Linux.Using grep to Find a Specific Word in a File
By default, grep searches through the contents of files as well as their file names. It’s included on the majority of Linux systems and is generally identical across distros. That said, smaller or less powerful Linux boxes might prefer to run a different command, like
ack
.
Depending on how the file is encoded, grep may not always be able to look inside. But for most text-based formats, grep can scan the text of the file for the specified pattern.
grep -Rw '/path/to/search/' -e 'pattern'
The
-R
flag sets grep to recursive mode, navigating through all the directories contained within the specified directory. The -w
flag searches for whole word matches. This means that ‘red’ will match only ‘red’ surrounded by whitespace characters and not ‘redundant’ or ‘tired.’ The -e
flag prefaces the pattern to search for. It supports regular expressions by default.
To speed up grep, you can use the
--exclude
and --include
flags to limit the search to certain types of files. For example, --exclude=*.csv
will not search within any files with the .csv extension. --include=*.txt
, on the other hand, will only search within files with the .txt extension. The flag can be added immediately after the grep command, as shown below:grep --exclude=*.csv -Rw '/path/to/search' -e 'pattern'
You can also exclude specified directories by following the format below:
grep --exclude-dir={dir1,dir2,*_old} -Rw '/path/to/search' -e 'pattern'
This command will not search in any directories in the present working directory named dir1, dir2, or matching the pattern *_old, eliminating them from the search process. It will execute the specified recursive, full-word match search on all other files in the present working directory.
Using find to Find a Specific Word in a File
While the
find
command’s syntax is more complicated than grep, some prefer it.find . -name "*.php" -exec grep "pattern" {} \;
This command will use find’s
-exec
flag to pass the found files to grep for searching. With a clever arrangement of syntax, you can use find’s faster file-system search to locate the specific file types you want to search within, then pipe them to grep in order to search inside the files.
Note that find only looks at filenames, not contents. That’s why grep is required to search file text and contents. The normal grep flags should be fully operational from within the
-exec
flag.Using ack to Find a Specific Word in a File
The ack command is likely the fastest searching tool, but it’s not as popular as the above options. The command below will search within the current directory.
ack 'pattern'
If you want to search within a specific file or directory, you can append that file or fully-qualified pathname to your search.
ack 'pattern' /path/to/file.txt
Conclusion
For most folks in most situations, grep is the best widely-available search tool. You can also look at other search tools, like RipGrep, for faster searching options
Comments
Post a Comment