Find – Search for Files and List Matches Only Once

file searchfindlocate

I want to search for arbitrary file/directory names, but only want to list file paths containing the search string at the same position once.
Especially not every file within a directory matching the search string.

Here is an example, locate -i flatpak lists:

/etc/flatpak
/etc/dbus-1/system.d/org.freedesktop.Flatpak.SystemHelper.conf
/etc/flatpak/remotes.d
/etc/profile.d/flatpak.sh
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/74
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/75
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/76
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/77
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/78
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/79
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/7a
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/7b
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/7c
/home/simon/.cache/gnome-software/flatpak/installation-tmp/repo/objects/7d
/var/lib/flatpak
/var/lib/flatpak/.changed
/var/lib/flatpak/.removed
/var/lib/flatpak/app
/var/lib/flatpak/appstream
/var/lib/flatpak/exports
/var/lib/flatpak/repo
/var/lib/flatpak/runtime

But I want a search result like this:

/etc/flatpak
/etc/dbus-1/system.d/org.freedesktop.Flatpak.SystemHelper.conf
/etc/profile.d/flatpak.sh
/home/simon/.cache/gnome-software/flatpak
/var/lib/flatpak

And which tool is best suited for this? locate, find, fd-find?

Best Answer

Sounds like you want to search for flatpak in the file name only (and not in other path components), so you can use the -b/--basename option:

So:

locate -ib flatpak

Another approach could be to use the -r/--regex option and write:

locate -ir 'flapak[^/]*$'

That is flatpak followed by any number of characters other than / followed by the end of the file path.

That might however miss filenames that have non-characters (in the current locale) after flatpak.

Related Question