2

I need to get the folder contents in one command line, right now if I do stat or ls, it tells me file type is Symbolic Link but it doesn't tell me if it's a file or a folder.

I'm using this;

stat -c '{"name": "%n", "size": "%s", "perms":"%a","type":"%F","user":"%U", "dereference","%N"}' /*;

Important point is, i need a one liner and very speedy output. I couldn't get around this doing ls, maybe there is a solution using find, locate etc. Or if u know how to read from mlocatedb ?

Thanks,

4 Answers 4

1

Try this which adds another field:

stat -c '{L"name": "%n", "size": "%s", "perms":"%a","type":"%F","user":"%U", "dereference":"%N"}' /* | 
    sed '/\/\o47\"\}$/ {s/\}/,\"dir\":\"yes\"\}/;b}; s/\}$/,\"dir\":\"no\"\}/'

By the way, I changed the comma after "dereference" to a colon.

6

ls -l will show you the target of a link. Is that what you need? Another option is readlink <file>.

Oops, sorry, didn't read everything there. How about ls -lL. The -L tells ls to dereference the link, so you'll see the target there instead of the link.

4

stat also knows the option -L to dereference symlinks. Try your calls with this parameter.

3
  • Is that anything like "defenestrating"? Jan 11, 2010 at 19:57
  • Sorry. I've taken a look at a dictionary. I'm not a native english speaker.
    – Christian
    Jan 11, 2010 at 20:27
  • Sorry, just kidding around. Jan 13, 2010 at 4:37
0

You could try using find with xargs ...something like this

find <PATH> -type l | xargs -I{} -t 'find {} -type d;'

which will

  • find all links
  • pass the result to another find which is restricted to subdirs only

You may have to tweak this a bit since I don't know your context, and I don't have a sample set of files that matches your needs to test it on There is a very good post here describing the mechanism

You must log in to answer this question.

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