15.08.2023

How To Find Files by Content in Linux

Introduction

Managing tasks in Linux might initially seem challenging due to several factors inherent to the operating system's design and philosophy. While Linux offers incredible power and flexibility, its unique characteristics can contribute to a perception of complexity for newcomers:

However, it's important to note that the perceived difficulty of Linux management diminishes as users become familiar with the system. Many resources, tutorials, and communities are available to help users learn and navigate Linux effectively. Additionally, the command line offers efficiency and precision once commands are learned. While Linux might seem challenging initially, it becomes highly rewarding as users gain confidence and experience in managing the system. Also for more simplicity manage there is utilities for find needed data by command!

Requirements

Installation

First of all we need to update all packages in the system and install them:

apt update && apt upgrade -y

Screenshot №1 — Update OS

For our purpose we will use grep utility that can help scale up broad and range of our option. For search needed information we will use various of attributes. By the default we already have that utilities in our repositories or installed in our operating system, check by typing commands below:

grep --help

Screenshot №2 — Help

If you will see options for command then you have installed there before, now we can use utility grep in single way. For that type command below:

grep -r "tt" /etc

The system will search through the /etc directory and its subdirectories for occurrences of the string "tt" in any files. If any matches are found, the corresponding lines containing the "tt" pattern will be displayed in the terminal as output.

Please note that searching through system directories like /etc might require administrative privileges, so you might need to use the command with elevated permissions (using sudo) depending on your system configuration.

grep -v "tt" /root/sometextfile.txt

In that combination of options we match whole words only, / meaning root directory and -name have sense looking for format and name. Also we can use invert request, just type -v option for searching all content exclude pattern and option -l displayed the names of files which contains the pattern:

grep -w -r "tt" /

We can use that while we searching amount file of content apt list for example:

apt list | grep nginx

In our case sign | means pipe or operator that give output from first to the second command as argument:

Screenshot №3 — Search by grep

As the result we can see file with mentions nginx in the header of files or if we use option with exact match of searching word:

apt list | grep -w nginx

Screenshot №4 — Exact word for search

Conclusion

Linux provides many different utilities for working with system modules, one of which we have reviewed in this article. Using the syntax and the above options, you will be able to search for and more efficiently perform the tasks assigned to you when working with files.