Find files containing a text under OSX or Linux

There are two methods I mainly use to find a text in multiple text files i.e. my project directory. If one is just generally looking for any file that may contain the text snippet I use grep:


grep -r "some_method_name" .

This will search all files recursively in your current path. If you only want to search in a sub directory from your current path just replace the dot with the path to the sub folder e.g.: grep -r "some_text" my/log/files

If I know or only want to see it for a certain type of file I’ll use find and xargs e.g. like this:


find . -name "*.erb" | xargs grep "some_method_name"

Again if you only want to search in a given sub directory just replace the dot with the path to the sub folder.

I found these two little bash lines real time savers!

Advertisements

Setting bash environment variables in Ruby

Ruby scripts can be great every day helpers. I recently run over a task in which I wanted to start an application that relied on exported variables. In Ruby one can easily do this via the ENV object.

ENV['ENV_VAR_NAME'] = "some/lib/path"

You can make this of course more dynamic by using the ARGV for command line arguments or config files. But I think you get the idea from the above example.

Please note that it is not possible under Ruby to export a variable to its parent process i.e. the caller shell with the example above. I do also not know of any way how that would be achieved.