commandlinefu.com is the place to record those command-line gems that you return to again and again.
Delete that bloated snippets file you've been using and share your personal repository with the world. 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.
If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/
You can sign-in using OpenID credentials, or register a traditional username and password.
First-time OpenID users will be automatically assigned a username which can be changed after signing in.
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
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:
There are 5 alternatives - vote for the best!
If you can do better, submit your command here.
You must be signed in to comment.
Why not just:
alias findstring="find . -type f -iname "*$1*"
??
I don't know, you tell me ..
Your command didn't work on my box, no output.
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.soIt's not for filenames, it's for searching for strings inside files.
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.
!!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 argv1alias ekko='echo $1'ekko testAlso, you need to use the `-print0 | -0` construct -
the completely correct version would be:
alias findstring='find . -type f -print0 | xargs -0 grep -H'Oh yeah.
Ok, so if its to grep files, then why not something like:
grep -r "$1" ./*@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
Fair enough
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
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" }@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@dezza test]$ function findstring () { grep -H "$1" **/"$2" }
>
This happens when I try to assign the function, have to terminate with CTRL+C
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$