Ubuntu – Excluding hidden files in locate

bashcommand linelocateregexsearch

We have a Ubuntu NAS sharing SMB and Netatalk with some Macs and I often use locate to find my files. Unfortunately since installing Netatalk it has written tons of .AppleDouble cache files to share with AFP faster (I assume) and my locate prints all of that information. I'm constantly using cp on files only to find I've copied a useless hidden file on accident.

I have been using locate -i filename | fgrep -v ".AppleDouble" | fgrep -v "._" to remove those hidden files, but I'd like to change my bashrc in such a way that this is more or less default.

What is the most effective way to exclude hidden files from what locate prints? With regex? Right now, I would write a script that passes an argument to locate and pipes to grep as shown, but if there's an easier way, please let me know.

Best Answer

To exclude hidden files when using locate, try this:

locate -i --regex "^/absolute/path/to/the/directory/[^\.]+"

If the directory contains files like .hidden, ..hidden, ...hiden they will be excluded too. If you just want to exclude only .hidden, remove the + from the end:

locate -i --regex "^/absolute/path/to/the/directory/[^\.]"

or simply (extended regexp is not needed too):

locate -ir "^/absolute/path/to/the/directory/[^\.]"

EDIT: After editing the question, the whole meaning of the question is different now and my initial answer is not correct in the modified context. Run the following to exclude all hidden files from the Output of locate (given the filename does not contain /):

locate -i "file_name" | egrep -v "/(\.)+[^/]+$"
Related Question