Eg, 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?
使用功能:
myfind() { find . ! -path "./build" -name "$1"|xargs grep "$2"; }
You can't do that with an alias. You could write a small script eg
#!/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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.