Using the grep Command in Linux: Finding Text & Strings in Files

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

In this guide, you’ll learn how to use the grep command. When performing administrative tasks on your Linode, many commands will give you more information than you need. Using grep allows you to filter that output in order to find only the data that’s relevant.

  1. To search a file for a particular string, provide the string and filename as arguments:

    grep 'some text' /etc/ssh/sshd_config
    
  2. You may also redirect output from a command to grep using a pipe:

    tail -f /var/log/apache/error.log | grep 'some text'
    

    This will monitor your Apache error logs, and display only the lines of output that contain the given string.

  3. Regex patterns are also supported by the -E option if you want to search for a set of strings rather than one literal:

    grep -E "[[:alpha:]]{16,20}" /etc/ssh/sshd_config
    

    This example will search the /etc/ssh/sshd_config file for strings of alphabetic characters that are 16-20 characters long, but you can use any regex pattern you like.

These are simply a few basic ways to use grep. Many other options exist, and in combination with other tools, it serves as an invaluable utility for performing administrative tasks on your Linode. For more information on some of grep’s advanced features, check out our guide on how to search and filter text with grep.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.