7

When I run the ls -l command I get details about the files in the current directory like permissions, owner, group, file size.

Is there a way I can get these same details instead of just the location of the file when I run locate?

3 Answers 3

5

The locate database do not contain information other that names, so you should run some other tool, like ls, on locate output

lsloc() {
  locate "$@" | 
    while read -r name; do
      ls -ld "$name"
    done
}
lsloc pattern
5

Pipe the output of locate to xargs (adding ls -l as parameters to xargs). This lets you use whatever options you need with either command. By also passing the -0 (dash-zero) option to both commands, you also protect against unintended interpretations of blanks or newlines in filenames. For example, to get the details of all .iso files on my system:

locate -0 -r '.*.iso$' | xargs -0 ls -l
3

Use a subshell

ls -l "`locate -b 'search_pattern'`"

Or

ls -lh "$(locate -b 'search_pattern')"

You can add a function to ~/.bashrc if you wish

function lsloc () { ls -l "$(locate -b "$1") ;}

You then

lsloc "search pattern"
6
  • 1
    Do not work if results contain spaces.
    – enzotib
    Dec 28, 2011 at 18:31
  • Yes it does, but as you know files with spaces are more complex. lsloc 'file\ with\ space'
    – Panther
    Dec 28, 2011 at 18:53
  • I talked about locate output, not pattern.
    – enzotib
    Dec 28, 2011 at 19:10
  • I understand, but, as you know, bash does not like file names with spaces, and the output of locate includes spaces. I modified my post and it now works with files with spaces.
    – Panther
    Dec 28, 2011 at 19:56
  • No, it still don't work every time you have more than one result.
    – enzotib
    Dec 28, 2011 at 22:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .