Quick findstring recursively in dirs (Alias from long find with xargs cmd)

alias findstring="find . -type f -print | xargs grep $1"

-4
By: dezza
2009-06-10 20:48:54

What Others Think

Why not just: alias findstring="find . -type f -iname "*$1*" ??
DaveQB · 866 weeks ago
I don't know, you tell me .. Your command didn't work on my box, no output.
dezza · 866 weeks ago
Its just using the name of the file pattern matching that find does, rather than piping it out to use grep for the same purpose. I would assume its more system efficient. It won't output anything, if there is no file to match. [david.ward@om012234 ~/dev]$ ll cgfxShader.so -r-xr-xr-x 1 david.ward sysadmins 394578 May 20 14:09 cgfxShader.so [david.ward@om012234 ~/dev]$ find . -type f -iname "*cgfxsh*" ./cgfxShader.so
DaveQB · 866 weeks ago
It's not for filenames, it's for searching for strings inside files.
dezza · 866 weeks ago
some implementations of grep won't output the filename if it's searching a single file, and sometimes xargs passes single filenames to grep. So, either add -n2 to the xargs call or -H to the grep call.
rwadkins · 865 weeks and 6 days ago
!!Critical Error!! functions and shell scripts can use "$1" - aliases should *NOT* to expose the bug, you would have to set a static positional parameter when you invoke bash... bash -s argv1 alias ekko='echo $1' ekko test Also, you need to use the `-print0 | -0` construct - the completely correct version would be: alias findstring='find . -type f -print0 | xargs -0 grep -H'
asmoore82 · 865 weeks and 6 days ago
Oh yeah. Ok, so if its to grep files, then why not something like: grep -r "$1" ./*
DaveQB · 865 weeks and 6 days ago
@DaveQB: yes, your last command does the same thing, but there's still a good purpose for having this alias. On some older implementations of grep, the -r (recursive) option is not implemented (Older HP-UX for example). Upvoted cause I found myself in need for this command a couple times
skinp · 865 weeks and 5 days ago
Fair enough
DaveQB · 865 weeks and 3 days ago
asmoore82: Thanks alot for your correction, I also discovered when playing some more with aliases that you could not use $1 and what I had done was only because I had the perception that it would work for aliases and so it did until I tried with 2 parameters and found out I was wrong all along. Will you please explain the `-print0 | -0` construct ? And what about the bash command thing you talked about earlier in your comment, does it enable using $1 like in scripts?
dezza · 864 weeks and 5 days ago
@dezza the -print0 and -0 combo between find|xargs says that all filenames (that is, strings) passed out of find are null-terminated (via -print0) and xargs expects the input strings to be null terminated (via -0). This covers the case when filenames have spaces. You could also use the multi-argument -exec option of find so I think the command would be: find . -type f -exec grep -H {} "search string" \; Although instead of an alias you could us a bash function which does support arguments, although you have to be a little careful about quoting. I have a command in my bashrc for grepping many files: function grepfiles () { grep -lr "$1" $2; } use the --include option to restrict the search to .c files or whatever. In bash 4.0 and zsh (and possibly in other shells) there is the ** globbing syntax which means "at any depth" so potentially you could accomplish what you are trying to do with: function findstring () { grep -H "$1" **/"$2" }
bwoodacre · 864 weeks and 2 days ago
@asmoore82 find . -type f -exec grep -H "searchy bandit" {} \; I think you should put the search string after grep, your way doesn't work for me, it thinks your search argument is the filename. If I use your grepfiles function like: grepfiles STRING2FIND * It only returns one result, not all containing "STRING2FIND" Which --include option are you talking about? I don't see --include in grep --help ..
dezza · 864 weeks and 1 day ago
[dezza@dezza test]$ function findstring () { grep -H "$1" **/"$2" } > This happens when I try to assign the function, have to terminate with CTRL+C
dezza · 864 weeks and 1 day ago
I now tried with bash 4.0 and readline 6.0 and this is what happens: [dezza@dezza ~]$ findstring stylesheet *.php grep: **/something.php: No such file or directory [dezza@dezza ~]$ in .bashrc function findstring() { grep -H "$1" **/"$2"; } If I try with function findstring() { grep -H "$1" "$2"; } It finishes instantly and doesn't find anything. If I try with function findstring () { grep -H "$1" **/"$2" } bash: /home/dezza/.bashrc: line 12: syntax error: unexpected end of file bash-4.0$
dezza · 863 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: