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"
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:For example:
And if you happen to have spaces (or other hazardous characters) in the $PATH (the
<<<
trick borrowed from the answer of @fedorqui):It also handles the empty $PATH correctly.
A bit clumsy: