Skip to content

Instantly share code, notes, and snippets.

@danielevans
Last active January 31, 2019 22:47
Show Gist options
  • Save danielevans/5275f6db654a585a966dcc0828f9e0de to your computer and use it in GitHub Desktop.
Save danielevans/5275f6db654a585a966dcc0828f9e0de to your computer and use it in GitHub Desktop.
Examples of how to search for source code using the unix tools find, grep and xargs

The guts:

find

find is a utility that searches for files and directories matching given criteria. You can have it match on file name, type, differentiate links vs. real files, filter based on size and so on.

-type f

This limits find to only return files and not directories.

-print 0

Normally, find just returns a giant stream of text with each file separated by a newline. When you dump that text as arguments to another program (via xargs) those programs can't tell the difference between spaces in the file names and new file names. the -print 0 option tells find to put a null character between arguments, thus making it easy for xargs to chunk those into discreet arguments using its -0 option.

-iname

This tells find to only return files matching a specific pattern using shell matching style, case insensitive. The -o parameter means or and allows you to put multiple matchers inside the parenthesis to match multiple kinds of files, in this case *.js and *.ts.

xargs

xargs is a utility that takes a stream of text and passes it as arguments to another program. It is a simple way of taking a stream of text such as that produced by find and passing it as command line options to programs to operate on it like grep

It can be configured to specifically format the incoming text, limit it to only one argument at a time or 10 or all of them for each execution of the target program.

-0

Treats the incoming stream as though it will be null delimited instead of dumping it directly to the program.

grep

grep searches through a stream of text or a file for a given regular expression pattern.

-i

Makes the match case insensitive.

# This is a fancier and more reliable method, it only searches .js and .ts files, supports files and directories with spaces in the name
# but is more complicated to remember and type.... In other words I'm lazy, this is more work that most of the time isn't needed
find lib node_modules \( -iname "*.ts" -o -iname "*.js" \) -type f -print0 | xargs -0 grep 'my_method_name'
# This is a simple, quick to type and easy to remember form
# this finds all files in lib/ and src/ that have my_method_name (case insensitive with the -i)
find src lib -type f | xargs grep -i 'my_method_name'
# pros: easy to remember and write quickly
# flaws: doesn't work if the file or directory names have spaces in them and this searches every file regardless of type
# I can filter this to only a few file types by making it look like this:
find src lib -type f | grep -i '\(\.js\|\.ts\)$' | xargs grep -i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment