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).
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
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.
A bit clumsy:
find $(echo $PATH | tr : ' ') -name \*SRA\*