Troskyvs
Troskyvs

Reputation: 8047

Linux shell: How to give parameter to alias of find command?

E.g, I wish to lookup current directory(exclude ./bin) for all files named "Makefile", as argument and grep "library", like below:

find . ! -path "./build" -name "*Makefile"|xargs grep library

I don't with to type all these each time, I just want to make an "alias" with 2 parameters, like:

myfind "*Makefile" "library"

myfind's find parameter is fo "find" command as "name",the second parameter for "grep" command, also I wish the asterisk "*" can be passes as part of parameter, without being parsed.

How to write such an alias?

Upvotes: 1

Views: 2539

Answers (3)

Lars Fischer
Lars Fischer

Reputation: 10149

You used the tag linux, so I assume that you are using GNU grep. GNU grep (at least grep (GNU grep) 2.25) supports the following options, which make your task very easy. Let me cite from grep --help:

 -r, --recursive           like --directories=recurse
 --include=FILE_PATTERN  search only files that match FILE_PATTERN
 --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
 --exclude-from=FILE   skip files matching any file pattern from FILE
 --exclude-dir=PATTERN  directories that match PATTERN will be skipped.

So your task can be accomplisehd by:

grep -r --include=Makefile --exclude-dir=build library

Of course you can create a shell script or function to further simplify that.

Upvotes: 1

Ipor Sircer
Ipor Sircer

Reputation: 3141

Use function:

myfind() { find . ! -path "./build" -name "$1"|xargs grep "$2"; }

Upvotes: 3

Michael
Michael

Reputation: 44150

You can't do that with an alias. You could write a small script e.g.

#!/bin/bash
find . ! -path "./build" -name "$1"|xargs grep $2

Save it as myfind somewhere on your path and make sure to remove the alias.

Upvotes: 1

Related Questions