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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



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
Quick findstring recursively in dirs (Alias from long find with xargs cmd)

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 458 weeks and 1 day ago

I don't know, you tell me ..

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

Comment by dezza 458 weeks and 1 day 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.

[[email protected] ~/dev]$ ll cgfxShader.so -r-xr-xr-x 1 david.ward sysadmins 394578 May 20 14:09 cgfxShader.so [[email protected] ~/dev]$ find . -type f -iname "*cgfxsh*" ./cgfxShader.so
Comment by DaveQB 458 weeks and 1 day ago

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

Comment by dezza 458 weeks and 1 day 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 458 weeks and 1 day 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 458 weeks and 1 day ago

Oh yeah.

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

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

Fair enough

Comment by DaveQB 457 weeks and 4 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?

Comment by dezza 457 weeks ago


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 456 weeks and 4 days ago


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 456 weeks and 3 days ago

[[email protected] 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 456 weeks and 3 days ago

I now tried with bash 4.0 and readline 6.0 and this is what happens:

[[email protected] ~]$ findstring stylesheet *.php

grep: **/something.php: No such file or directory

[[email protected] ~]$

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


Comment by dezza 455 weeks and 3 days ago

Your point of view

You must be signed in to comment.