How to Search for Text in Files on Ubuntu

search text files ubuntuLooking for a way to search for text in files on Ubuntu? You should get a grip on the Ubuntu grep command.

Ubuntu grep command searches input FILE or FILES for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

For your information grep stands for Global Regular Expression Print.

So do I need to know regular expression to use grep?

For simple searching you don’t need to be a regular expression guru. Remember the simplest regular expression (regex) is simply a word, or more generally, a string of characters.

Search for the given text in a single file

The basic usage of ubuntu grep command is to search for a specific string (text) in the specified file as shown below.

grep "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.

grep has returned two lines containing our search term “this” in the demo file.

Checking for the given string in multiple files.

grep "string" FILE_PATTERN
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.

In this example we have used two files demo_file and demo_file1 in the current directory containing same content. See how grep has printed the name of the file first then the line containing search term.

Searching in all files recursively

You can use -r option if you want to search in all the files under the current directory and its sub directory. The following example will look for the string “geeks” in all the files in the current directory and all it’s subdirectory.

$ grep -r "geeks" *

Case insensitive search using grep -i

grep -i "string" FILE

This searches for the given string/pattern case insensitively. So it matches all the words such as “geeks”, “GEEKS” and “Geeks” case insensitively.

Till now we have searched for specific string/text in file or files. What happens when we don’t know the exact text to search?

Here comes Pattern or Regular Expression. We can search for a pattern if they are crafted in regular expression with grep.

Match regular expression in files

grep "REGEX" filename

A regular expression (REGEX or REGEXP) is a special text string for describing a search pattern. This one page guide of regular expression will quickly get you up to speed with regular expressions.

Need more geekary? This is a linux command repository of the most elegant and useful linux commands. Great commands can be shared, discussed and voted.

67 thoughts on “How to Search for Text in Files on Ubuntu

  1. Wow, that’s good because a friend shared with me about the difficulties in the Search for Text in Files on Ubuntu. I will share this information with her article.

  2. Parce que tout cela est naturel et il ne vous obligent pas
    à faire un régime énorme ou vivre votre vie dans le gymnase, il semble que lorsque vous lisez un Garcinia cambogia examen, c’est un produit qui est bon pour
    n’importe qui.

  3. In addition to raising Serotonin amounts, Garcinia Cambogia Extract
    with HCA blocks the enzyme, Citrate Lyase, which prevents ingested sugar and carbs
    from turning to fat.

  4. If readers here miss the St.Patricks’ Day deal, then they may not miss our
    two exclusive coupon codes from Hostgator, which might apply to all Hostgator internet hosting packages.

  5. A primary baby plan provides unlimtied area, bandwidth,
    domain names and over 48 extra companies for advance web developers, allowing you to run the most complex type of websites.

  6. Read some articles and critiques for internet hosting providers and you’ll begin to pick out those that
    have a fame for providing great service.

  7. Some servers hosts questionable web sites your I.P.
    deal with will likely be associated with. This means your site
    might be banned in certain countries or by certain customers because
    of the content current on the server you share.

  8. Although it is likely to be barely dearer, the consolation of realizing
    that you can depend on your internet host might be effectively
    worth it.

  9. Uncover a internet host that lets you merely upgrade your website and makes it potential for for additional bandwidth and
    disc area as you’ll want it. Get your HostGator coupon and you’re able
    to go!

  10. Some net hosts only offer 1GB of free space, whereas others offer
    much more. Especially without cost websites, the extra space you utilize,
    the more limited your account will become till you
    start paying for it.

  11. Regardless of how nice your content or how authentic your site design, if your net host
    isn’t up to the duty, no one will ever see it. The options above will
    show you how to consider hosting corporations and make sensible decisions about
    who you entrust together with your web hosting needs.

  12. For people shopping for primary internet hosting ideas (shared offers) with a
    month to month billing, the most effective HostGator coupon is HGGREEN2011.

  13. Or when paying out yearly prematurely, redeem the HostGator coupon HGATOR25OFF to get
    25% low cost from preliminary bill which helps make
    spectacular $89.fifty five price discount a yr for the Child

  14. They do all of the switch work of moving your web site database,
    recordsdata, domains, email accounts and so forth., if you transfer to Hostgator
    from another net host.

  15. Hostgator is without doubt one of the best internet hosting companies since it really is 1 of essentially
    essentially the most dependable, no matter ones want,
    1 could be optimistic that they’re very experienced with it.
    Changing into as massive as they’re, they will afford to present some great
    saving’s codes and promotions which are unparalleled within the market.

  16. Hostgator position way more highly effective restrictions
    on the a number of sites referring to the same server in evaluation to most different
    internet extensive selection suppliers.

  17. The disk area out there for HawkHost Basic is at least three GB.
    In the meantime, HostGator Hatchling has unlimited
    bandwidth and disk house obtainable.

  18. Finally, quite a lot of free hosting services do not give you the choice of having your own domain name.
    In case you are critical about your business, the price of acquiring paid internet hosting shouldn’t
    be significant in any respect contemplating the advantages
    you will receive.

  19. Utilizing these (and I say these” as there are various totally different Hostgator Coupons and Codes) will permit you to get your hosting package deal from Hostgator at
    a big low cost, particularly if you happen to join 2 or 3 years!

  20. Databases- Hostgator presents Limitless MySQL Databases
    with phpMyAdmin Entry and also supports CGI, Fast CGI, PHP 5, Ruby on Rails, Perl,
    Python, SSI, Curl, CPAN, GD Library, ImageMagick, SSH Entry and
    Cron Job Scheduling.

  21. Sure, whichever coupon you select to make use of, somebody will be benefiting financially from it.
    I’m simply being completely clear with you.

  22. The bundle additionally comes with a 45 cash-again guarantee, as well as 24/7 support from the
    knowledgeable Hostgator workers and instantaneous backups for maintaining all your
    recordsdata utterly protected.

  23. Some folks contemplate the forms of content material administration systems that an online host affords,
    and that is why many individuals select to host their web sites utilizing Hostgator.

  24. For year 2011, one of the vital priceless coupon code is SPRING which supplies substantial preliminary discounts in accordance with consumers number of subscription technique.

  25. Don’t be deterred from utilizing a website hosting service that
    employs customer support brokers abroad. Although it might be obscure various
    accents at times, notice that many countries spend extraordinary quantities
    of time and energy in making their citizens technologically superior.

  26. The most preferrred stage is the extent three because it will probably deal with the needs of a rising business and prices solely $forty four.ninety two within the initial
    month and then may be renewed at $forty nine.95 and provides a 1.thirteen GHz CPU, 30 GB of disk space
    768 MB of RAM and a Bandwidth of 500 GB.

Leave a Reply

Your email address will not be published.