TransWikia.com

How to list attributes (esp. date) of a symbolic link's target file?

Unix & Linux Asked by ysap on January 2, 2021

When typing ls -la we get the list of files in the current directory, with symlinks having also their target files printed.

When displaying a symlink, the shell prints the attributes (and particularly, the date) of the symlink itself.

Is there an option to print the attributes of the target?

2 Answers

add options -dLl to ls.

-L tells ls do show target. -d tells it to not enter a directory (only makes a difference if the file is a directory). -l says to do a long list (with detail).

Correct answer by ctrl-alt-delor on January 2, 2021

Most system calls and applications, when given the path of a symlink, do perform their operation on the target of the symlink. Notable exceptions are the ones that operate on directory entries rather than files such as unlink()/rm, link()/symlink()/ln.

When it comes to getting file metadata information, there are two separate system calls: lstat() (operate on symlink itself), stat() (operate on target of symlink). With the newer statx() on Linux, that's with a AT_SYMLINK_NOFOLLOW flag instead. See also listxattr()/llistxattr(), getxattr()/lgetxattr() for extended attributes.

And when it comes to utilities that wrap those, the default depends on the utility, and the option if any to change the mode of operation also depends on the implementation.

ls by default, operates on the target of the file given as argument, unless passed any of the -d, -l, -F, -g, -o, -n options, in which case it operates on the symlink itself (uses lstat() instead of stat() or uses statx() with the AT_SYMLINK_NOFOLLOW flag).

If you want to use one of those options but still want to follow symlinks, you can pass the -H option.

ls -ldH -- "$file"

Will report information about the file after symlink resolution.

ls also has a -L option which tells it to resolve all symlinks, not only those for the files given as argument. For instance, ls -Ll /symlink-to-dir would resolve /symlink-to-dir to the directory it links to, and list its contents, and for each symlink in there, would also list the information at the target of the links. It also affects how recursion is done with -R.

For other tools:

Command               | default   | change with
----------------------+-----------+--------------
   IRIX stat          | target    | -L
    zsh stat          | target    | -L
    GNU stat          | symlink   | -L
    BSD stat          | symlink   | -L
busybox stat          | symlink   | -L
    AIX istat         | target    | none
    GNU date -r       | target    | none
    GNU find -printf  | symlink   | -follow/-L/-H
  Linux attr          | symlink   | -L
  Linux getfattr      | target    | -h
  Linux getfacl       | target    | -P

Some examples, for the modification time specifically:

$ date -r link +%FT%T.%N%z # GNU date
2020-03-19T17:57:11.277584965+0000
$ zmodload zsh/stat
$ stat -F %FT%T.%N%z +mtime link
2020-03-19T17:57:11.277584965+0000
$ gstat -Lc %y link # GNU stat
2020-03-19 17:57:11.277584965 +0000
$ ls -lHd --full-time link # GNU ls
drwxr-xr-x 23 root root 29 2020-03-19 17:57:11.277584965 +0000 link/
$ find -H link -prune -printf '%TFT%TT%Tzn' # GNU find
2020-03-19T17:57:11.2775849650+0000

Answered by Stéphane Chazelas on January 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP