Tags

, , ,

How to use grep to find text including sub directories:
grep -rl {yoursearchstring} {pathtosearch} e.g.,

$ grep -rl "hello" /var/
/var/lib/dpkg/status-old
/var/lib/dpkg/status
/var/log/test.log:1:hello world

where:
-r or –recursive – is used to traverse also all sub-directories of /path
l or –files-with-matches – used to only print filenames of matching files, and not the matching lines (this could also improve the speed, given that grep stop reading a file at first match with this option).

Another way to search that will return the line matching files.

$ grep -Hrn "hello" /var/
/var/lib/dpkg/status-old:10:hello world
/var/lib/dpkg/status:13:hello world
/var/log/test.log:1:hello world

How to find/search a specific file from a directory:
find {directory} -iname {searchstring}
where:
iname – search by search string (case insensitive )
E.g.,

$ find rails-blog-heroku/ -iname *.bak
rails-blog-heroku/app/assets/javascripts/application.js.bak
rails-blog-heroku/app/assets/stylesheets/application.css.bak
rails-blog-heroku/app/assets/stylesheets/auth.css.scss.bak
rails-blog-heroku/app/assets/stylesheets/welcome.css.scss.bak
rails-blog-heroku/app/views/article/add.bak
rails-blog-heroku/app/views/article/edit.bak
rails-blog-heroku/app/views/devise/registrations/new.bak
rails-blog-heroku/app/views/devise/sessions/new.bak
rails-blog-heroku/app/views/layouts/application.html.erb.bak
rails-blog-heroku/Gemfile.bak

How to delete a files from find search results:
Search all the bak files and delete it.
$ find rails-blog-heroku/ -iname *.bak | xargs rm

Advertisements