Bash scripting question with find and grep and .bashrc - a multi year problem
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash scripting question with find and grep and .bashrc - a multi year problem
No lie.. I've tried to solve this one off and on for years, I'll mess with it for awhile, get frustrated, give up.. so let me ask here as I still can't figure this out.
I have this script which works PERFECTLY for me below, I can search all the PWD and all directories below from where I call it and it will search all *.c and *.h files for the text I put after the -n at the end. PERFECTLY...
However, like alot of things in linux, that is a very long and cumbersome command to type so I stuck it into my .bashrc file like this...
alias ff="find $(pwd) -name '*.[ch]' -print0 -type f | xargs -0 grep -n"
It now works differently if I use it as follows..
ff CREEPING_CRUD
instead of working from the PWD and below, it continues checking everywhere on the file system, and usually NOT where I called if from, but elsewhere. This sometimes makes a simple search of a small branch of a directory tree take minutes instead of seconds.
HOW do I get the alias (or whatever method works better) to do exactly the same thing to happen which happens on the command line (i.e. search from PWD and below, not everywhere)? Aside from just fixing this, I would like to understand WHY it is doing this as I am sure there are ramifications to this that will whack me in the future with other things.
I still haven't tried the soft link, I really have to give that a try. I've only ever used soft links for directories.. thanks for helping me ever expand my limited but growing knowledge in Linux.. BTW.. rather than escaping the inner ' I just "" and it seems to work fine.
alias ff='find $(pwd) -name "*.[ch]" -print0 -type f | xargs -0 grep -n'
1000 thanks, as stupid as it may sound, this has been kicking me for years, I really haven't done that much scripting, but its something that worth taking the time for.. now to find that time..
Thanks again all.. I swear this forum has never ever let me down.. thanks for that!!!
Hi chrism01,
Yeah I had originally used the . but I had the same problem.. rather than reverting back to using the ., I figured that $(PWD) was pretty unambiguous (okay.. so is the dot)... but the problem was where it was expanded at the time the alias was made (I guess) as neonsignal said.
Hi HunnyKhan3,
Post your .bashrc maybe I can tell you. I am sure you know that logging in as root will give you the # and the bash-3.2 is the default.. so it looks like you killed that somehow. Post and lets see.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.