Find all files whose names contain a given string, and highlight it

半世苍凉 提交于 2019-12-24 13:53:06

问题


I find myself writing this command a lot:

find . -iname "*foo*" | fgrep -i "foo"

To find all files and folders that have "foo" in their names, and to highlight the matching part in the results.

That's not very convenient. What would be a simpler solution? Do I need to write a custom command for this?


回答1:


For convenience you can create a BASH function for this:

hlt() { find . -iname '*'"$1"'*' | grep --color "$1"; }

and call it as:

hlt foo



回答2:


if you're using bash take a look at Make a Bash alias that takes a parameter? for how to make "custom command" also note that grep without -i is case sensitive while -iname is not so you might be missing some mixed case hilights. On the side note fgrep should be faster than grep and probably less resource hungry :)



来源:https://stackoverflow.com/questions/33389064/find-all-files-whose-names-contain-a-given-string-and-highlight-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!