Monday, August 08, 2011

Bash function of the day

I push the command line on people, because it's generally the most efficient way to do several different things. My devs resist me, although I don't understand why... part of it is that most places I've worked at are Windows houses. No problem, say I: Cygwin to the rescue. Seriously, gang, I'm not just making this up.

(I'll grant, however, that there are some Finder/Explorer replacements that wrap up some of this functionality pretty nicely in a GUI, including having a command line in whatever directory is open--but it's still a command line, and you still should know how to use it.

Two things I do a lot from the command line are find files, and find files containing a pattern. Here's two drop-dead simple Bash functions that do this. Seriously, doesn't get much easier.

function fn {
  find . -type f -name "$1"
}


function fng {
  find . -type f -name "$1" | xargs grep "$2"
}

2 comments:

prodrive555 said...

If "fn" is "find" what is "fng"?

Also have you heard about/seen ack -> http://betterthangrep.com/?

Dave Newton said...

Effin' New Guy?

find-grep. Yeah, I have ack, and could wrap it up in a function too. The raw command to search *.rb files for 'model' would be:

ack -r -G '\.rb$' model

I'd rather use globbing for naive filenames, I guess.