1

Suppose you have file file with a given mtime. If I create a symlink sym pointing to file via ln -s the symlink itself gets a mtime-stamp that corresponds to the time where the symlink was created which is not the same as the mtime of file.

So is there a way to create a symlink such that it has the same mtime as the file it is pointing to?

The background to this question is this script, where a directory of links to the n newest files of a cloud mirror is created. Now if I do for example ls -lt in this current directory, it doesn't show me the dates where the actual files were created but when the symlinks were created. But I want to see the files creation date.

Edit In my original post I erroneously asked for ctime (I changed it above), however the question should be about mtime. Thanks to @ilkkachu indicating this.

0

3 Answers 3

2

Do you mean ctime or mtime? Ctime is the time of the latest change to the the file metadata (the inode contents) or the file contents. It updates on writes, but also if e.g. the permissions are changed. Mtime is the time of the latest write to the file data, and it can be changed with touch, while ctime usually can't.

Because it can be reset for copies, the mtime is usually the more useful one. It's also the one that ls -lt shows (you'd need to add -c to show the ctime).

You could use touch -h to change the mtime of a symlink:

$ ls -lt
total 4
lrwxrwxrwx 1 ilkkachu ilkkachu 9 Sep  4 15:10 link.txt -> hello.txt
-rw-r--r-- 1 ilkkachu ilkkachu 6 Dec 31  1999 hello.txt

$ touch -h -r hello.txt  link.txt 
$ ls -lt
total 4
-rw-r--r-- 1 ilkkachu ilkkachu 6 Dec 31  1999 hello.txt
lrwxrwxrwx 1 ilkkachu ilkkachu 9 Dec 31  1999 link.txt -> hello.txt

but that still needs to be done manually, it doesn't update automatically if the targeted file changes. But you could use ls -L to have it print the details of the targeted file, and not the link itself:

$ touch hello.txt 
$ ls -L -lt
total 8
-rw-r--r-- 1 ilkkachu ilkkachu 6 Sep  4 15:12 hello.txt
-rw-r--r-- 1 ilkkachu ilkkachu 6 Sep  4 15:12 link.txt

Of course it also hides the fact that it's a link to begin with.

If you need mixed output, with the link name and target shown with the dates and ownership of the pointed-to file, you may need to use something like Perl to create that listing.

1

None of ln, touch or the syscall utimensat enable the ctime to be changed, only mtime & atime.

If this is something you really need to do, then your only option would be to roll your own syscall/kernel module/userspace exe.

0

No, symbolic links don't really have any useful metadata (timestamps, owner, perms) of their own.

You can, however, use -H with ls to dereference (i.e. follow) symlinks. Or -L with stat.

Or you can use readlink -e as in my answer to your linked question to get the full canonical pathname of whatever it is that they symlink is pointing to.

You must log in to answer this question.

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