How to find a specific word in a file on Linux

The famous GNU search program, grep can search inside the file with the correct flag. This article will show you how to find specific words in a file on Linux

By default, most search engines will search for files or directories without looking for file content. However, the famous GNU search program, grep can search inside the file with the correct flag. This article will show you how to find specific words in a file on Linux.

  1. Search for files and directories in Linux using the command line interface
  2. 7 commands to manipulate the most basic files and folders everyone must know
  3. 10 notable Linux file managers

1. Use the grep command to find specific words in a file

By default, grep searches for content as well as file name. It is available on most Linux systems and distributions. However, less powerful and smaller Linux versions may prefer to run other commands like ack.

Depending on how the file is encrypted, grep may not search within the file but for most text-based formats, it can scan the text in the file to find the specified pattern.

 grep -Rw '/ path / to / search /' -e 'pattern' 

How to find a specific word in a file on Linux Picture 1How to find a specific word in a file on Linux Picture 1

The -R flag sets grep in recursive mode, navigating through all the directories contained in the specified directory. The -w flag searches for words that match the conditions in the command. For example, the word red matches only red surrounded by a space character. The flag provides a search pattern. It supports regular expressions by default.

To speed up the search process, you can use the --exclude and --include flags to limit the search to a specific file type. For example, --exclude=*.csv will not search for files with file extensions .csv and --include=*.txt will only search for files in the .txt extension. You can add the flag immediately after the grep command as follows:

 grep --exclude = *. csv -Rw '/ path / to / search' -e 'pattern' 

Alternatively, you can remove the specified directory in the format below:

 grep --exclude-dir = {dir1, dir2, * _ old} -Rw '/ path / to / search' -e 'pattern' 

This command will not search the directory named dir1, dir2 or match the pattern * _old in the search process. It will perform a full, recursive word search on all other files in the scan directory.

2. Use the find command to search for specific words in a file.

How to find a specific word in a file on Linux Picture 2How to find a specific word in a file on Linux Picture 2

Although the find command's syntax is more complicated than grep, some users prefer to use it.

This command will use the -exec flag of find to transfer the found files to grep to search for content in the file. With clever syntax arrangement, you can use the faster file system search tool of find to determine the specific file type you want to search in, then convert them to grep to search within the file.

You need to note that find finds only the file name not the file content. It is necessary to use grep to search for text in the file. The find command only helps to find file types faster.

3. Use the ack command to find specific words in a file

The ack command can be the fastest search engine but not as common as the above two commands. The command below will search in the current directory.

 ack 'pattern' 

If you want to search for specific files or directories, you can add the file or the qualified path to the search.

 ack 'pattern' /path/to/file.txt 

I wish you all success!

4.7 ★ | 3 Vote