DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Trending

  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Designing AI Multi-Agent Systems in Java
  1. DZone
  2. Data Engineering
  3. Data
  4. Find All Files Containing a String or Text on Linux and Mac

Find All Files Containing a String or Text on Linux and Mac

Let's look at how grep works on Linux and Mac, to find specific strings of text.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Mar. 08, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
11.4K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes on Linux/UNIX computers, you will want to do a search for any files containing a particular string within it. This can be particularly useful when searching for specific things for a CI/CD pipeline.

On Linux or Mac, if we want to search a set of files for a specific string in a terminal, we use the grep.

Find All Files Which Contain a Particular String on Linux or Mac

Return the Filename Only

The below will find any file that contains the term 'html' in the '/views' directory. It will return only the file names.

PowerShell
 
grep -rl ./views -e 'html' 

Return the Text Itself

Again, this will find any file containing the string 'html' in the '/views' directory. It will return the line which has that text in it.

PowerShell
 
grep -r ./views -e 'html' 

How To Use Grep on Linux/Mac

You can string other options together, to get different results. When we say -r, for example, we mean 'recursive' - i.e. it will search through every folder. When we write -rl, this means essentially -r -l, which means search recursively, and return only the file name.

Below is a list of all grep options or switches, which you can add to your query to get the results you need:

  1. -r - search recursively.
  2. -l - return only the file name.
  3. -i - ignore the case.
  4. -w - search only for words, i.e. not text within words. For example, if we search for 'html', then somehtmltext would not match.
  5. -n - returns the line number, but doesn't work with -l.
  6. -s - suppress any error messages.
  7. -h - output the line itself, without the line number or file.
  8. -v - invert the search, i.e. searching for html with -v will return everything without html.
  9. -f - used to indicate a file you want to use which contains a regular expression.
  10. -x - match only if the whole lines only. This will only return for a search of html if that exists on its own line separately.
Strings Data Types Linux (operating system)

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!