pythongrepripgrep

Finding the distinct directory names using grep or rg


I am new to Linux commands and I am trying to search for a word that say "apple" recursively in a directory and all the directory names that contain the word.

I am using ripgrep(rg) similar to grep to get the file names that have an apple in them

Ex:

rg -l "apple" 

gives me output:

a/fruit.c
b/fruit.c
a/fruits.c
a/fruitsnames.c
a/fruity/fruits.c
b/fru/fruit/fru/fr.c
c/frts.c

Is there a way that I can only the unique parent folder names?

Example output I expect is to get only the folder names:
a
b
c

Solution using grep is also good.

Thank you so much


Solution

  • use sed to remove everything from /, then use sort -u to remove duplicates

    ripgrep -l apple | sed 's#/.*##' | sort -u