0

I'm trying to use locate to find any files on my system that contain the word 'Jaynes'. Unfortunately there's a symbolic link called Jaynes that points to a directory. I want to exclude all symbolic links and directories from my locate search. Obviously, I can do this with find but it's slower.

To be specific, the output from this command in my bash script

ls -al `/usr/bin/locate -i Jaynes`

is

-rw-r--r-- 1 simon simon     80 Aug 10  2016 /home/simon/LOCALSVN/ward/trunk/literature/Jaynes/JaynesBook.html
lrwxrwxrwx 1 simon simon     49 Oct 24  2016 /home/simon/research/Monash/Ward/literature/Jaynes -> /home/simon/LOCALSVN/ward/trunk/literature/Jaynes

/home/simon/LOCALSVN/ward/trunk/literature/Jaynes:
total 1352
drwxr-xr-x 2 simon simon   4096 Aug 10  2016 .
drwxr-xr-x 6 simon simon   4096 Oct 21  2016 ..
-rw-r--r-- 1 simon simon     80 Aug 10  2016 JaynesBook.html

What I'm trying to do is eliminate the reference to the symbolic link (the second line) and also to the lines below which follow the symbolic link, leaving only the first line, which is a real file.

Thanks very much for any advice

Leo Simon
  • 1,529

1 Answers1

1

locate itself does not have an option to filter out links (it can only follow or not follow links. You can filter out links using something else:

locate() {
    command locate -0 "$@" |      # print filenames separated by \0
      while IFS= read -rd '' f    # read filenames separated by \0
      do
          [[ -l "$f" ]] ||        # test for links
             printf "%s\n" "$f"
      done
}

Save this in your .bashrc; then in a new shell, locate -i Jaynes will not list links.

muru
  • 197,895
  • 55
  • 485
  • 740