Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to acess ctime, mtime, … of a symbolic link?

On unix symlinks are pointers to another file. Not only the file but also the symlink has a ctime, mtime, …. I know the symlinks time can be accessed, as ls displays it. If I use one of ruby's File#ctime, File#mtime, …, I always get the attribute of the file the symlink is pointing to, not of the symlink. How can I read this values in ruby? If this is not possible in ruby, tell me how to do it in C. I would write my own c extension in that case.

like image 942
johannes Avatar asked Jan 05 '10 22:01

johannes


People also ask

How do I change the timestamp on a symlink?

delete the old symlink you wish to change 1. change the system date to whatever date you want the symlink to be 2. remake the symlink 3. return the system date to current.

How do I change the date on a symlink?

You need to add the '-h' parameter to modify symbolic link timestamps. Without the '-h' the timestamp of the file that is linked to is modified.

How do I change a timestamp on a link in Linux?

Setting specific timestampsUse the -d ( --date= ) option to specify a date string and use it instead of the current time. The date string needs to be enclosed in single quotes. For example, the following command will set the last access and modification times of file1 to 1 June 11:02 of the current year.

What is timestamp in Linux?

Timestamps are records for the times in which actions are performed on files. A timestamp is useful because it keeps records of when a file was accessed, modified, or added. Linux's files have 3 timestamps recorded by the computer: Access timestamp (atime): which indicates the last time a file was accessed.


1 Answers

Use File#lstat(). Example:

# This is a dummy symlink; there's no file named "foo".
ln -s foo bar

# Run irb.
irb(main):001:0> File.lstat("bar")
=> #<File::Stat dev=0x801, ino=90113, mode=0120777, nlink=1, uid=1000, gid=1000, rdev=0x0, size=3, blksize=4096, blocks=0, atime=2010-01-05 17:59:06 -0500, mtime=2010-01-05 17:59:05 -0500, ctime=2010-01-05 17:59:05 -0500>

# Get the mtime of the link.
irb(main):002:0> File.lstat("bar").mtime
=> 2010-01-05 17:59:05 -0500
like image 183
John Feminella Avatar answered Sep 30 '22 09:09

John Feminella