Fedora Linux Support Community & Resources Center
  #1  
Old 14th February 2011, 05:08 PM
drinkingcoffee Offline
Registered User
 
Join Date: Jun 2009
Posts: 46
linuxfirefox
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:

Code:
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:

Code:
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:

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

Code:
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.
Thanks!
Reply With Quote
  #2  
Old 14th February 2011, 05:58 PM
DBelton Offline
Administrator
 
Join Date: Aug 2009
Posts: 7,320
linuxfedorafirefox
Re: bash function alias to recursively grep current directory

try enclosing your exclude string in single quotes.
Reply With Quote
  #3  
Old 14th February 2011, 06:11 PM
drinkingcoffee Offline
Registered User
 
Join Date: Jun 2009
Posts: 46
linuxfirefox
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.

Doing
Code:
unalias rg
fixes things. fun!
Reply With Quote
Reply

Tags
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 03:59 PM
bash alias not working pushparaj_bits Programming & Packaging 9 15th November 2010 01:19 PM
Making an alias for current users Nagglfar Using Fedora 2 14th March 2009 04:13 AM
Start a new bash whose current directory is changed fmoreau Using Fedora 7 23rd January 2008 09:35 PM


Current GMT-time: 18:14 (Saturday, 25-10-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

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
Fort Dauphin Instagram Photos - Guadeloupe - Churachandpur Photos