Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

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
share|improve this answer
add comment

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
share|improve this answer
add comment

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"
share|improve this answer
1  
Do not work if results contain spaces. –  enzotib Dec 28 '11 at 18:31
    
Yes it does, but as you know files with spaces are more complex. lsloc 'file\ with\ space' –  bodhi.zazen Dec 28 '11 at 18:53
    
I talked about locate output, not pattern. –  enzotib Dec 28 '11 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. –  bodhi.zazen Dec 28 '11 at 19:56
    
No, it still don't work every time you have more than one result. –  enzotib Dec 28 '11 at 22:29
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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