find executables in my PATH with a particular stri

2019-07-14 03:31发布

Is there a way to quickly know whether an executable in my $PATH contains a particular string? For instance, I want to quickly list the executables that contain SRA.

The reason I'm asking is that I have several scripts with the characters SRA in them. The problem is that I always forget the starting character of the file (if I do remember, I use tab completion to find it).

标签: bash shell
3条回答
叛逆
2楼-- · 2019-07-14 04:10

You can store all the paths in an array and then use find with its various options:

IFS=":" read -ra paths <<< "$PATH"
find "${paths[@]}" -type f -executable -name '*SRA*'
  • IFS=":" read -ra paths <<< "$PATH" reads all the paths into an array, setting the field separator temporarily to :, as seen in Setting IFS for a single statement.
  • -type f looks for files.
  • -executable looks for files that are executable.
  • grep -l just shows the filename if something matches.

Since the -executable option is not available in FreeBSD or OSX, ghoti nicely recommends to use the -perm option:

find -perm -o=x,-g=x,-u=x
查看更多
forever°为你锁心
3楼-- · 2019-07-14 04:18

For example:

find ${PATH//:/ } -maxdepth 1 -executable -name '*SRA*'

And if you happen to have spaces (or other hazardous characters) in the $PATH (the <<< trick borrowed from the answer of @fedorqui):

tr ":\n" "\\000" <<< "$PATH" | \
  xargs -0r -I{} -n1 find {} -maxdepth 1 -executable -name '*SRA*'

It also handles the empty $PATH correctly.

查看更多
走好不送
4楼-- · 2019-07-14 04:21

A bit clumsy:

find $(echo $PATH | tr : ' ') -name \*SRA\*
查看更多
登录 后发表回答