How can I force grep to only return files and ignore directories?
Question
I'm trying to use grep to automatically locate the bibtex program in the user's bin folder.
The problem is, it frequently returns a directory with the name bibtex, which means that the script fails to automatically run the command.
How can I force grep (or indeed the locate command) to automatically ignore directories?
EDIT: The current command is: locate bibtex | grep "/bibtex$" -d skip | head -n1
Solution
The find
command?
find /bin -name bibtex -type f
Looks for a file name "bibtex" of type "f", which is a regular file.
OTHER TIPS
locate --basename '\bibtex' --limit 1
Eliminates the need for grep
and head
, but still doesn't solve the issue of whether the result is a directory.
Why don't you do:
type -P bibtex
which will look in the PATH
for a program by that name?
I don't understand exactly so maybe my solution is wrong: why don't you use which? Or bibtex isn't in PATH?
Sigh, not my cleanest, but it works. perl -ne 'chomp($f=$_);print if !-d $f'
which makes your command locate bibtex | perl -ne 'chomp($f=$_);print if !-d $f' | grep "/bibtex$" -d skip | head -n1