InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Finding Files by Content in bash
Bash

Finding Files by Content in bash

InfinityCoder February 20, 2017

How do you find a file of some known content?

Let’s say that you had written an important letter and saved it as a text file, putting .txt on the end of the filename.
Beyond that, the only thing you remember about the content of the letter is that you had used the word “portend.”

If you are in the vicinity of that file, say within the current directory, you can start with a simple grep:

1
grep -i portend *.txt

With the -i option, grep will ignore upper- and lowercase difference.

This command may not be sufficient to find what you’re looking for, but start simply.

Of course, if you think the file might be in one of your many subdirectories, you can try to reach all the files that are in subdirectories of the current directory with this command:

1
grep -i portend */*.txt

Let’s face it, though, that’s not a very thorough search.
If that doesn’t do it, let’s use a more complete solution: the find command.

Use the -exec option on find so that if the predicates are true up to that point, it will execute a command for each file it finds.

You can invoke grep or other utilities like this:

1
find . -name '*.txt' -exec grep -Hi portend '{}' \;

We use the -name ‘*.txt’ construct to help narrow down the search.

Any such test will help, since having to run a separate executable for each file that it finds is costly in time and CPU horsepower.

Maybe you have a rough idea of how old the file is (e.g., -mdate -5 or some such).

The ‘{}’ is where the filename is put when executing the command.

The \; indicates the end of the command, in case you want to continue with more predicates.
Both the braces and the semicolon need to be escaped, so we quote one and use the backslash for the other.

It doesn’t matter which way we escape them, only that we do escape them, so that bash doesn’t misinterpret them.
On some systems, the -H option will print the name of the file if grep finds something.

Normally, with only one filename on the command, grep won’t bother to name the file, it just prints out the matching line that it finds.

Since we’re searching through many files, we need to know which file was grepped.
If you’re running a version of grep that doesn’t have the -H option, then just put /dev/ null as one of the filenames on the grep command.

The grep command will then have more than one file to open, and will print out the filename if it finds the text.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Running All Scripts in a Directory
You want to run a series of scripts, but the …

Running All Scripts in a Directory

Clearing the Command Hash in bash
You need to make sure that your command hash has …

Clearing the Command Hash in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap