Linux – Using locate to only find directories containing the string “foo”

linux

Is is it possible to use the locate command to only find directories? To rephrase, I want to search for a directory, and exclude any result that is not a directory, such as files or symlinks to files.

While not necessary, I would like the solution to also return symlinks to other directories (but again, not files).

Lastly, I would like to exclude directories within foo, with one exception. If a subdirectory of foo also contains a subdirectory with a name containing foo, I would prefer it be returned as well.

If the following files and directories exist…

/home/foo/
/home/foo/foo.txt
/home/foo/someotherdir/
/home/foo/someotherdir/food/
/food/
/var/www/foo -> /home/foo/
/var/www/food -> /home/food/index.html

Output would only contain…

/home/foo/
/home/foo/someotherdir/food/
/food/
/var/www/foo -> /home/foo/

Thanks in advance.

Edit: Thank you for the responses. I am very deliberate in titling this for the locate command and not find.

Best Answer

Maybe a litte tricky but here it comes:

locate foo | xargs file -NF '|' |grep '| directory' | sed 's/| directory//g'