How find only executable files using ‘locate’

locate

locate gtags would find all the files named gtags.

What if I only need executables, is there any way to do this?

Best Answer

Not easily. You can use

locate bash | while IFS= read -r line; do [[ -x "$line" ]] && echo $line; done

to find all executables where the name contains bash. This is faster than using find across the whole filesystem because only a few files need to be checked.


  • locate bash does what it always does (lists all matches)
  • | (pipe) takes the output from the first command (locate) and sends it to the second one (the rest of the line)
  • the while ...; do ... done loop iterates over every line it receives from the pipe (from locate)
  • read -r line reads one line of input and stores it in a variable called line (in our case, a path/file name)
  • [[ -x "$line" ]] tests whether the file in $line is executable
  • if it is, the && echo $line part prints it on your screen
Related Question