Subject: Re: bug#18179: stat doesn't dereference symlinked file name

From: Bob Proulx <[email-masked]>
Date: Sun, 3 Aug 2014 15:39:33 -0600

tag 18179 + notabug
close 18179
thanks

Thanks for the bug report.  However this seems to be a
misunderstanding of the available print formats.

Tomasz Pala wrote:
> ~: touch a
> ~: ln -s a b
> ~: stat -c "%n" a
> a
> ~: stat -c "%n" b
> b
> ~: stat -Lc "%n" b
> b

You need %N to show what you are wanting to show.

  $ stat -c %N b
  ‘b’ -> ‘a’
  $ stat -Lc %N b
  ‘b’

> ~: stat -Lc "%n" b
> b
> the last command should return 'a'.

Not quite.  The %n returns the file name as handed to stat on the
command line.  To get the quoted name dereference if a symbolic link
then you need the %N format.

         %N     quoted file name with dereference if symbolic link

> To be more verbose:
> 
> ~: stat b
>   File: `b' -> `a'
>   Size: 1               Blocks: 0          IO Block: 4096   symbolic link
> Device: 10h/16d Inode: 60112068    Links: 1
> 
> ~: stat -L b
>   File: `b'
>   Size: 0               Blocks: 0          IO Block: 4096   regular empty file
> Device: 10h/16d Inode: 60112022    Links: 1
> [...]
> 
> one cannot get the righthand part of dereference only (the same applies
> to %N).

I read the above part several times but your meaning was not clear.
In any case I see no bug in stat.

Perhaps you really want readlink?

  $ readlink b
  a

  $ readlink -f b
  /tmp/a

For what I believe you are doing the typical idiom is to use
"readlink" to print the value of the symbolic link.

Bob