Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
List all Files with Specific Extension in R
Next article icon

How to Find all Files Containing Specific Text (string) on Linux

Last Updated : 31 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Suppose you are looking for a file in Linux, but you have forgotten its name. You only remember the contents of the file. How will you find the file in this case? Well, there are some useful methods that will help you find a file containing a specific text (or string) in Linux. The string needs to be specified by the user. So, let's take a look at the methods:

Methods to Find All Files Containing Specific Text (string) on Linux

Method 1: grep command

grep command in Linux that is used to search for files containing a specific text or string. By default, it shows us the lines in the files that contain the particular text. If we append the -l option to it, the command will show us all the files that contain the particular text.

Example:

Suppose, we have a directory that contains two files named file1.txt and file2.txt.

Contents of file1.txt:

This line contains text.

Contents of file2.txt:

You should learn Data Structures & Algorithms.

Now, we will use the grep command with the -l option to search for text in given files located inside the current directory. See the following example:

It can be clearly said from the above example that the grep command has successfully found the given string in file1.txt. As a result, it displayed the file name on the screen.

We can also use the -i option to tell grep to ignore the case. Look at the following example:

Above, we have first used the previous command, but the given string is Text. Because file1.txt contains text, not Text, it is not taken into consideration. Here, the search operation is performed keeping the case in mind. Then, we used the -i option. As a result, the case is ignored and the given string matches with the one that file1.txt contains. Hence, the file name is displayed on the screen.

Another variation is to use the -r option. It suggests grep to search for the given string in the current directory and its subdirectories recursively. Look at the below example:

file1.txt and file2.txt are located in the files folder, not in the current directory, i.e. desktop. So, if we don't use the -r option, no files with matching strings will be found because they don't exist in the current directory. But we used the -r option and also omitted the file names. As a result, grep searches for matching strings in not only the current directory but also in its subdirectories as well. Hence, file1.txt is found and displayed on the screen.

Method 2: The combination of find and grep command

find is another useful command in Linux. We will combine find with the -type f option to search for files and the -exec option to apply to grep on the files that are found. Look at the following example:

Clearly, the search operation finds file1.txt as it contains the matching string. Hence, the file name is displayed on the screen.

Method 3: Find files containing specific text with mc

We can also search for files using Midnight Commander (mc). Open the application and press Alt + Shift + ? to open the Find File dialogue box. You will see a Starting box at the top. In the box, type the path where the files exist. Then, under the content box, type the string you want to search. In our case, we searched for text in the Files directory:


It can be clearly seen below that the search operation has successfully found file1.txt, which contains the matching string.

Method 4: ripgrep command

ripgrep (written as rg) is a command that can be used as an alternative to the grep command. The implementation is below:

file1.txt is found and hence, the file name is displayed on the screen.

Method 5: ack command

Yet another command we can use is the ack command. Here is the implementation:

file1.txt is successfully found and displayed on the screen.


Next Article
List all Files with Specific Extension in R

S

sriparnxnw7
Improve
Article Tags :
  • Technical Scripter
  • Linux-Unix
  • How To
  • Technical Scripter 2022

Similar Reads

  • Find an element that Contains Specific Text in Python Selenium
    In Selenium WebDriver, one of the key tasks when automating web applications is finding elements that contain specific text. This is especially useful when we need to verify that certain text is displayed on a page or interact with elements based on the text they contain. While Selenium provides mul
    4 min read
  • List all Files with Specific Extension in R
    R programming language contains a wide variety of method to work with directory and its associated sub-directories. There are various inbuilt methods in R programming language which are used to return the file names with the required extensions. It can be used to efficiently locate the presence of a
    3 min read
  • How To List All Commits That Changed A Specific File In Git?
    Git, a widely used version control system, allows developers to track changes in their codebase efficiently. One common need when working with Git is to list all commits that have modified a specific file. This can be particularly useful for tracking changes, understanding the history of a file, or
    1 min read
  • How to find if an array contains a specific string in JavaScript/jQuery ?
    In order to Check if an array contains a specific string be done in many ways in Javascript. One of the most common methods to do this is by using the traditional for loop. In Javascript, we have several other modern approaches which help in finding a specific string in an array to iterate the array
    5 min read
  • Find line number of a specific string or substring or word from a .txt file in Python
    Finding the line number of a specific string and its substring is a common operation performed by text editors or any application with some level of text processing capabilities.  In this article, you will learn how to find line number of a specific string or substring or word from a .txt (plain tex
    4 min read
  • How to Find and Remove Duplicate Files on Linux?
    Most of us have a habit of downloading many types of stuff (songs, files, etc) from the internet and that is why we may often find we have downloaded the same mp3 files, PDF files, and other extensions. Your disk spaces are unnecessarily wasted by Duplicate files and if you want the same files on a
    4 min read
  • How to Find Document with Array that Contains a Specific Value in MongoDB
    MongoDB is a popular choice for developers working with large volumes of data, due to its flexibility and powerful querying capabilities. One common task developers face is finding documents in a collection where an array field contains a specific value. In this beginner-friendly article, we'll expl
    4 min read
  • How to Find All Failed SSH Login Attempts in Linux System?
    By monitoring failed SSH login attempts in Linux, a system administrator can easily maintain the security of the Linux System. Every time a user tries to log into a Linux System using SSH whether it is a successful attempt or a failed one, it is recorded as an SSH login attempt in the records of the
    5 min read
  • How to list All Files in a Commit in Git?
    Working with Git, it's often essential to inspect the contents of your commits. Whether you need to review changes, debug issues, or understand the history of your project, knowing how to list all files in a commit is a fundamental skill. In this article, we’ll walk you through the steps to list all
    3 min read
  • Find all the Files in a Directory with .txt Extension in Python
    Directory traversal is a common operation performed by file locator in Operating Systems. The operating system offers elaborate methods for streamlining the search process and the choice to search for a selected filename/extension. This article will teach you how to find all the files in a directory
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences