Find executables in my PATH with a specific line

Is there a way to quickly find out if the executable in my $PATH contains a specific line? For example, I want to quickly list executables containing SRA .

The reason I ask is because I have several scripts with SRA characters. The problem is that I always forget the initial character of the file (if I remember, I use the tab to find it).

+1
source share
3 answers

You can save all the paths in an array, and then use find with various parameters:

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

Since the -executable option -executable not available on FreeBSD or OSX, ghoti recommends using the -perm :

 find -perm -o=x,-g=x,-u=x 
+2
source

For instance:

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

And if you have spaces (or other dangerous characters) in $ PATH (trick <<< , borrowed from @fedorqui's answer):

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

It also correctly processes an empty $ PATH.

+2
source

A bit awkward:

 find $(echo $PATH | tr : ' ') -name \*SRA\* 
+1
source

Source: https://habr.com/ru/post/1234799/


All Articles