1

when doing a locate to find a specific file, it shows alot of stuff, but is there a way to inverse what is shown? Ex. locate anything that has the word drupal in it, but don't show anything from the home directory.

  • 2
    I don't think locate command itself has any option for inverse but you can always pipe you output and filter out what you don;'t want to see with grep -v command using or use `find . -type -f not -name "pattern"' – Raza Sep 25 '14 at 20:16

1 Answers1

3

Yes, I do what @Raza suggests all the time. For example, let's say I want to find all font-related files not sitting in ~/installations/ waiting to be installed.

locate font | grep -v installations

Oops, I forgot that will match a lot of things in $HOME/.cache and $HOME/.config.

locate font | grep -v $HOME

Now to get rid of man pages, locale stuff, icons, ...

locate font | grep -v -e $HOME -e icons -e /man/ -e /locale/

Probably not interested in /var/cache either ...

locate font | grep -v -e $HOME -e icons -e /man/ -e /locale/ -e /var/cache

etc.

Another use case I often have is searching for all versions of a program on my system. locate emacs will spew out thousands of results, so I filter only for results where 'emacs' is in the basename:

locate emacs | egrep 'emacs[^/]+$'

D'oh, this still shows icons and man pages and whatnot. So how about where the basename has no period after the emacs ...

locate emacs | egrep 'emacs[^/.]+$'

This is much better.

Remember, unlike find, locate is a database lookup; it doesn't consume any file system resources to search for results, so you're not sacrificing anything by generating lots of results and then filtering them with grep.

dg99
  • 2,642