Jump to content

How to Search File Contents in Linux


Linux Hint

Recommended Posts

As a Linux administrator, you must look into the log files, configuration files, or scripts for some error messages or irregularities to troubleshoot it. This is the concept of searching the file contents which also helps when you cannot recall the file name but merely remembers a little bit of its content. Moreover, everything from the text files to large directories is considered a file in Linux, and its content includes every other file inside it. Hence, searching the contents of a file can be a simpler approach. However, many beginners are unaware on how to search the file contents and get errors. So, in this short blog, we will explain the simple way to search the file contents in Linux without any hassle.

How to Search the File Contents in Linux

You can search the file contents through various commands. So, let’s take a look at them one by one through easy examples.

The Grep Command

The global search for regular expression or “grep” command searches for an input text in the file contents.

grep -lir 'text to search'

Replace “text to search” with the string that you want; the -l option is for printing the files that consist of matching content. The “-i” option ignores the cases (uppercase and lowercase). However, if you know the case of the desired string, don’t use this option. The “-r” option directs the commands recursively to search the current directory and subdirectories.

For example, let’s find the files having the term “Fedora” in any directory.

grep -lir 'Fedora'

search-file-contents-linux-01.png

The following image shows that the “grep” command displays all the files with the desired content.

You can use the “grep” and “find” commands together to display the files including their path. This helps you in locating the files that you’re looking for. As its base operation is different and the “find” command cannot search inside a file, we must use the “grep” command alongside it.

find -exec grep 'text to search' {} \;

The “-exec” option executes the “grep” command with “find” in the previous command. For instance, if you want to find the files in the “Documents” directory that contains the term “josh,anoa”, the command will be:

Find -exec grep -lir 'josh,anoa' ./Documents {} \;

search-file-contents-linux-02.png

Conclusion

In today’s digital world, you would often need to look for files that have specific content. It can help you resolve the issues and errors and get the names of the files that you probably forgot. Considering these points, this guide explains how to search the file contents in Linux. We explored two commands with the help of examples. Moreover, the “find” command only looks for file names and cannot search the file contents, so we must combine it with “grep”.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...