C – Soft link atime and mtime modifications

Soft link atime and mtime modifications… here is a solution to the problem.

Soft link atime and mtime modifications

Is it possible to change the atime and mtime of the symbolic link?

I’m trying to change it using the utime() function (C code), but it changes the time of the target file.

If I did

cp -dpr <src fldr> <<dest folder> (command line) 

The [src folder contains different symbolic links]. ]
The symbolic link to the destination is created using the current timestamp.

Executing stat() on a

symbolic link gives me the time of the target file (in C code), but if we trigger the stat command on the command line, it gives the timestamp of the link (probably using lstat).

Any ideas?

Solution

You can use touch with the -h flag in bash to modify the mtime and atime of the symbolic link, not the file it references:

touch -h somesymlink

If you do not want to use the current time, you can use the -t flag to specify the time it is set to.

Related Problems and Solutions