AnswerBun.com

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

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?

Unix & Linux Asked by ysap on January 2, 2021

2 Answers

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!

Related Questions

How to upgrade rhel 7.2 to rhel 7.6 Server

0  Asked on February 10, 2021

       

Is there a way to reset updates on Debian (with apt)?

0  Asked on February 10, 2021 by coffeecatde

     

How to use GoToMeeting in Linux

4  Asked on February 8, 2021 by illarion-kovalchuk

 

How to activate v-sync with nvidia linux driver?

1  Asked on February 7, 2021 by chiseledabs

   

How to set country (region) for WiFi globally in Linux Mint 20?

1  Asked on February 6, 2021 by linuxsecurityfreak

     

lsblk doesn’t show attached virtual storage / partition

1  Asked on February 5, 2021 by robert-einsle

     

Samba with multiple shares from the same server

3  Asked on February 5, 2021 by s55michi

   

increase TrackPoint sensitivity beyond max

0  Asked on February 5, 2021 by modernneo

     

Can GRUB font size be customised?

5  Asked on February 5, 2021 by neydroydrec

     

Ubuntu route table: docker entry causes VPN to not resolver

1  Asked on February 5, 2021 by perimosh

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.