Creates a customized search command

alias cr='find . 2>/dev/null -regex '\''.*\.\(c\|cpp\|pc\|h\|hpp\|cc\)$'\'' | xargs grep --color=always -ni -C2'
Creates a command alias ('cr' in the above example) that searches the contents of files matching a set of file extensions (C & C++ source-code in the above example) recursively within the current directory. Search configured to be in colour, ignore-case, show line numbers and show 4 lines of context. Put in shell initialisation file of your choice. Trivially easy to use, e.g: cr sha1_init
Sample Output
./openssl/signatures.c-600-xmlSecOpenSSLDsaSha1EvpInit(EVP_MD_CTX *ctx)
./openssl/signatures.c:602:    return SHA1_Init(ctx->md_data);
./openssl/signatures.c-706-    NULL,
./openssl/signatures.c-707-#else /* XMLSEC_OPENSSL_096 */
./openssl/signatures.c:708:    SHA1_Init,
./openssl/signatures.c-709-    SHA1_Update,
./openssl/signatures.c-710-    SHA1_Final,

By: chrisdrew
2009-01-26 08:54:25

What Others Think

I appreciate the use of the --color switch to grep.
root · 670 weeks and 1 day ago
May want to use the -s switch to grep, or fix it so it handles paths with spaces. i.e.: alias cr='find . 2>/dev/null -regex '\''.*\.\(c\|cpp\|pc\|h\|hpp\|cc\)$'\'' | xargs grep --color=always -ni -C2' Someone with more talent could probably make this work with weird filenames rather than just cutting them out
kaedenn · 666 weeks and 2 days ago
doula los angeles I am amazed by the way you have explained things in this article. This article is quite interesting and I am looking forward to reading more of your posts. Thanks for sharing this article with us.
yeoyeo · 54 weeks and 3 days ago
great site! glad i found this. any recent updates? painters idaho falls
looneyhans · 40 weeks and 5 days ago
nt, till the activity was pretty done, and the footwear stood equipped to be used upon the desk. this become long before dawn; and then they bustled away as short as lightning.
socialwokl · 40 weeks and 4 days ago
socialwokl · 40 weeks and 4 days ago
seofox · 1 week and 4 days 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? 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.


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: