Hide

What's this?

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/

Get involved!

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.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

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

Terminal - Quick findstring recursively in dirs (Alias from long find with xargs cmd)
alias findstring="find . -type f -print | xargs grep $1"
2009-06-10 20:48:54
User: dezza
Functions: alias
-4
Quick findstring recursively in dirs (Alias from long find with xargs cmd)

Alternatives

There are 2 alternatives - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

What others think

Why not just:

alias findstring="find . -type f -iname "*$1*"

??

Comment by DaveQB 267 weeks and 4 days ago

I don't know, you tell me ..

Your command didn't work on my box, no output.

Comment by dezza 267 weeks and 4 days 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
Comment by DaveQB 267 weeks and 4 days ago

It's not for filenames, it's for searching for strings inside files.

Comment by dezza 267 weeks and 4 days 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.

Comment by rwadkins 267 weeks and 4 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'
Comment by asmoore82 267 weeks and 4 days ago

Oh yeah.

Ok, so if its to grep files, then why not something like:

grep -r "$1" ./*
Comment by DaveQB 267 weeks and 3 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

Comment by skinp 267 weeks and 3 days ago

Fair enough

Comment by DaveQB 267 weeks 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?

Comment by dezza 266 weeks and 3 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" }
Comment by bwoodacre 266 weeks 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 ..

Comment by dezza 265 weeks and 5 days 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

Comment by dezza 265 weeks and 5 days 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$

Comment by dezza 264 weeks and 6 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts