Tuesday, 7 February 2012

Using Find and Grep

find and grep are incredibly powerful. But many never learn a fraction of what they can do. Did you know that grep can print the surrounding lines? Or that you can search for files based on permissions? Here’s a collection of examples that might expand your notion of what can be done with find and grep.
Most find commands follows the pattern:
$ find [PATH] [expression]
where the expression consists of one or more of the options shown below. find is recursive by default so the command $ find . would output the entire directory tree at the current location.
Find files named Account.java in src directory:
$ find src -name Account.java
Find case insensitive in current directory — matches READMEreadmeReadMe, etc.
$ find . -iname readme
Find by wildcard name — NB: quotes keep shell from expanding wildcards, it is best to always include them.
$ find /etc -name '*xml'
Some exclusions — -not can be replaced with !
$ find . -not -name '*java' -maxdepth 4
Find with or
$ find . -name '*java' -o -name '*xml'
There are several ways to do stuff with the results of your find.  I prefer to use the more intuitive xargs rather than -exec, as the -exec syntax is strange. But -exec gives you the ability to choose whether you want the second command executed once (+) or once per result (\;).
$ find . –name '*.java' | xargs wc –l | sort
$ find . –name '*.java' -exec wc –l {} \; | sort
$ find . –name '*.java' -exec wc –l {} + | sort
It is often useful to filter by type. Using find is more reliable than parsing ls.
$ find . -type f
$ find . -type d
$ find . -type l
To find all directories in current directory:
$ find . -type d -maxdepth 1
Do you want to see the files that have changed today?
$ find . -mtime -1
Or the files that have changed in the last 15 minutes?
$ find . -mmin -15
Sometimes it is convenient to find the files that are newer (or older) than a certain file.
$ find . -newer foo.txt
$ find . ! -newer foo.txt
Of the files that have been modified after a certain date … or between two dates.
$ find . -type f -newermt '2010-01-01'
$ find . -type f -newermt '2010-01-01' ! -newermt '2010-06-01'
You can also filter by size. Note that the - finds smaller files, + finds larger files.
$ find . -size -1k
$ find . -size +100M
I don’t know that I’ve every included permissions in a search, but it could be useful.
$ find . -perm 644
$ find . -perm -u=w
$ find . -perm -ug=x
grep command typically takes the form
$ grep [options] [string/regex] [file or path]
For example:
$ grep 'new Account' *.java
Note that you do not quote a wildcard expression for grep, in this case we want the shell to expand the expression.
grep is not recursive by default, this is altered with the -r flag.
$ grep -r 'Dao[Impl|Mock]' src
I use the following flags commonly:
-i Case insensitive
-w Restricts to word matches
-n outputs line number
-c outputs count of matches
Grepping for multiple terms is useful in theory, but I rarely do it:
$ grep -e foo -e bar baz.txt
Whether searching through source code or log files, the ability to display the surrounding lines is often useful. For example:
$ grep -r -A 2 foo src
will display the lines that contain foo and the two subsequent lines. Similarly, you can use
-B before
-C centered (lines both before and after found text.)
That’s all for now, I hope that this helps you find something soon.

12 comments:

  1. This is very good content you share on this blog. it's very informative and provide me future related information.
    python training in pune | python training institute in chennai | python training in Bangalore

    ReplyDelete
  2. I was recommended this web site by means of my cousin. I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!
    Java training in Chennai | Java training in Bangalore

    Java interview questions and answers | Core Java interview questions and answers

    ReplyDelete
  3. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Data Science Training in Indira nagar | Data Science Training in Electronic city

    Python Training in Kalyan nagar | Data Science training in Indira nagar

    Data Science Training in Marathahalli | Data Science Training in BTM Layout


    ReplyDelete
  4. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.

    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete

  5. Easily, the article is actually the best topic on this registry related issue. I fit in with your Python training in pune conclusions and will eagerly look forward to your next updates.

    ReplyDelete
  6. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    artificial intelligence course in hyderabad

    ReplyDelete
  7. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.
    360Digitmg digital marketing training in hyderabad

    ReplyDelete
  8. HVAC & Plumbing Services
    Air Star Heating guarantees reliability and quality for all equipment and services.

    Air Star Heating specialists always try to deliver the most excellent quality of services to our customers at an affordable price. It is understood that every client has different needs and different problems. We try to accomplish the needs of every client according to their requests. We are having considerable experience in this field. Our specialists understand very well how things work. It doesn’t matter in which field of industry you are looking for services.
    Plumbing & HVAC Services in San Diego. Call now (858) 900-9977 ✓Licensed & Insured ✓Certified Experts ✓Same Day Appointment ✓Original Parts Only ✓Warranty On Every Job.
    Visit:- https://airstarheating.com

    ReplyDelete


  9. This post is so interactive and informative.keep update more information...
    Java Training in Bangalore
    Java Classes in Pune

    ReplyDelete
  10. This post is so interactive and informative.keep update more information...
    German Classes in Velachery
    German Classes in chennai

    ReplyDelete