×

How to Search and Find Files for Text Strings in Linux/Ubuntu? – LinuxWays

How to Search and Find Files for Text Strings in Linux/Ubuntu? – LinuxWays


There can be a situation when we need to search a file but we do not remember the name of the file but we do know the contents of the file. Linux/Ubuntu file managers do not allow searching files via text strings. File Managers only allow the search of a file by file name. In these situations, we can search all of the files containing a particular string by using command line tools in Linux/Ubuntu. Similarly, a string can also be searched in a file using command line tools.

In this article, we will demonstrate different ways to search for a string pattern in a file and to find all files containing a particular string pattern on Linux/Ubuntu 22.04 LTS by using the below commands/utilities:

  • Grep Command
  • Ripgrep Command
  • Ack Command
  • Midnight Commander

Method 1: Grep Command

grep (Global Regular Expression Print)” is a Unix/Linux command line tool used to search and filter out string patterns in a file or streams, etc. In this section, searching string patterns in a file and finding all files containing a string pattern is demonstrated using the grep command.

How to Search/Find a String Pattern in a File via grep Command?

The following command will be executed to search the string pattern “Congestion” in a file “samplefile1.txt”:

$ grep Congestion samplefile1.txt

From the above image, it can be seen that the grep command has filtered out the desired string pattern, i.e., “Congestion” by highlighting all the occurrences.

How to List Names of Matching Files Containing a String Pattern via grep Command?

The following command will be executed to list the name of all files that contain the string pattern “throughput”:

From the above image, it can be seen that the grep command has filtered out the files in the current directory that contains the searched string pattern, i.e., “throughput”.

Method 2: Ripgrep Command

ripgrep” command recursively searches for lines matching a pattern in the current directory. It is not pre-installed in Ubuntu 22.04. “ripgrep” can be installed by:

$ sudo apt install ripgrep

From the above image, it can be seen that ripgrep is installed successfully. In this section, searching string patterns in a file and finding all files containing a string pattern is demonstrated using the ripgrep command.

How to Search/Find a String Pattern in a File via ripgrep Command?

The following command will be executed to search the string pattern “Congestion” in a file “samplefile1.txt”:

$ rg “Congestion” samplefile1.txt

From the above image, it can be seen that the ripgrep command has searched for the desired string pattern, i.e., “Congestion” by highlighting all the occurrences.

How to List Names of Matching Files Containing a String Pattern via ripgrep Command?

The following command will be executed to list the name of all files that contain the string pattern “throughput”:

From the above image, it can be seen that the ripgrep command has searched the files in the current directory that contains the searched string pattern, i.e., “throughput”.

Method 3: Ack Command

ack” is a command-line tool alternative to the “grep” command. “ack” is used to search for string patterns in files and directories. “ack” is not pre-installed in Ubuntu 22.04. “Ack” can be installed by:

Press “Y” to continue with the installation process:

From the above image, it can be seen that “ack” is installed successfully. In this section, searching string patterns in a file and finding all files containing a string pattern is demonstrated using the “ack” command.

How to Search/Find a String Pattern in a File via Ack Command?

The following command will be executed to search the string pattern “Congestion” in a file “samplefile1.txt”:

$ ack Congestion samplefile1.txt

From the above image, it can be seen that the “ack” command has searched for the desired string pattern, i.e., “Congestion” by highlighting all the occurrences.

How to List Names of Matching Files Containing a String Pattern via Ack Command?

The following command will be executed to list the name of all files that contain the string pattern “throughput”:

From the above image, it can be seen that the “ack” command has searched the files in the current directory that contains the searched string pattern, i.e., “throughput”.

Method 4: Midnight Commander

Midnight Commander is a free-to-use file manager. Midnight Commander is not pre-installed in Ubuntu 22.04 and can be installed by:

Press “Y” to continue with the installation process.

From the above image, it can be seen that “Midnight Commander” is installed successfully. In this section, searching string patterns in a file and finding all files containing a string pattern is demonstrated using the midnight guard.

How to Search/Find for a String Pattern in a File via Midnight Commander?

The Midnight Commander can be launched by executing the following command:

After Midnight Commander is launched, the “Command” button can be clicked. A drop-down menu will then be opened and the “Find files” button will be pressed:

The following window will be launched. “Filename” and the string pattern to search can be entered in the “File name:” and “Content” fields respectively. Additionally, the search selection criteria can also be chosen. After all information is added, the “OK” button is pressed to search for the word “congestion” in “samplefile1.txt

The search will start and it will take a few seconds.

From the above image, it can be seen that there are five occurrences of “Congestion” in “samplefile1.txt”

How to List Names of Matching Files Containing a String Pattern via Midnight Commander?

All files in a directory that contains the string pattern “throughput” can be searched by using Midnight Commander by pressing the “Command”. A drop-down menu will then be opened and “Find files” button will be pressed:

The following window will be launched. The string will be entered in the “Content” field. The search selection criteria is then chosen. After all information is added, “OK” button is pressed:

The search will take a few seconds.

From the above image, it can be seen that there are a total of 6 occurrences of the string “throughput” in two files, ie., samplefiel2.txt and samplefile3.txt.

Conclusion

Command line tools such as “grep, “ripgrip”, “ack” and “midnight commander” are used to search for string patterns in a file and find all files containing a particular string pattern. In this article, we demonstrated different ways to search for a string pattern in a file and to find all files containing a string pattern on Linux/Ubuntu 22.04 LTS



Source link