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-601-{
./openssl/signatures.c:602:    return SHA1_Init(ctx->md_data);
./openssl/signatures.c-603-}
./openssl/signatures.c-604-
--
./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,

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

What Others Think

I appreciate the use of the --color switch to grep.
root · 613 weeks and 3 days 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 · 609 weeks and 5 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?

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: