I have the following version of locate:

$ locate --version
mlocate 0.26
Copyright (C) 2007 Red Hat, Inc. All rights reserved.
This software is distributed under the GPL v.2.

This program is provided with NO WARRANTY, to the extent permitted by law.

I am trying to find all files (not directories) that have some specific basename, e.g. python, so I've tried the following:

$ xargs -a <(locate -b '\python') -I{} file {} | sed -E '/directory|symbolic/d;s/:.*$//g'

This prints exactly the expected output. However, I wonder if there is an efficient way to achieve that instead?


Your commands also outputs files the current user doesn't have the permission to access.

Slightly shorter solution would be

locate -0b '\python' | perl -0nE 'say if -f'

but it doesn't print the non-accessible files.

You can use bash to loop over the files, too, but it's a bit more verbose:

locate -0b '\python' | while IFS= read -d '' -r f ; do
    [[ -f $f ]] && printf '%s\n' "$f"

Not the answer you're looking for? Browse other questions tagged or ask your own question.