Fedora Linux Support Community & Resources Center
Old 14th February 2011, 06:08 PM
drinkingcoffee Offline
Registered User
Join Date: Jun 2009
Posts: 46
bash function alias to recursively grep current directory

Hi -

This should be a simple thing to accomplish, but I can seem to figure it out.
Essentially, I want to have a bash alias or function that will let me recursively grep the current directory.

A while back I added this to my .bashrc:

alias rg="grep -r --exclude=\*/.svn/\* --exclude=\*.swp"
This works fine, (and also ignores any svn and vim swp files), and I can call it like:

rg foo *
However, 99.999% of the time, I am only interested in searching in the current directory, so the "*" is a bit redundant. Also, I would say 5-10% of the time, I am typing faster than thinking and forget the "*", so grep just sits there trying to read from stdin.

It's a pretty minor thing, but ideally I'd like to be able to just type:

rg foo
I've tried creating a function to handle this:

function rg(){
grep -r --exclude=\*/.svn/\* --exclude=\*.swp $1 *
but it behaves exactly the same as the alias above. escaping the "*" with \'s doesn't work, and neither does trying `pwd` (or even a hard-coded path) in its place.

My bash skills are pretty rudimentary, so I assume that I'm missing something basic here.
Reply With Quote
Old 14th February 2011, 06:58 PM
DBelton Offline
Join Date: Aug 2009
Posts: 7,334
Re: bash function alias to recursively grep current directory

try enclosing your exclude string in single quotes.
Reply With Quote
Old 14th February 2011, 07:11 PM
drinkingcoffee Offline
Registered User
Join Date: Jun 2009
Posts: 46
Re: bash function alias to recursively grep current directory

I think I just figured out the problem.
I guess that a bash 'alias' definition will take precedence over a function defined later on (this makes sense in retrospect).

What I was doing was: editing .bashrc, commenting out the alias rg="..." line, then adding a function of the same name.

Then, sourcing the new .bashrc was still leaving the "rg" alias active, so would not actually call the new function.

unalias rg
fixes things. fun!
Reply With Quote

alias, bash, current, directory, function, grep, recursively

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete certain type of files recursively in a directory leonard Using Fedora 5 26th April 2011 04:59 PM
bash alias not working pushparaj_bits Programming & Packaging 9 15th November 2010 02:19 PM
Making an alias for current users Nagglfar Using Fedora 2 14th March 2009 05:13 AM
Start a new bash whose current directory is changed fmoreau Using Fedora 7 23rd January 2008 10:35 PM

Current GMT-time: 05:58 (Thursday, 18-12-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Lac d'Hossegor - Kota Dompu, NTB Travel Photos - El Calafate - Patagonia Photos - U-do, Jeju, South Korea - Mile High Stadium Photos