I know how to change the timestamp of a regular file:
touch -t 201301291810 myfile.txt
I was not able to do the same with a symlink. Is it possible?
Distro: RHEL 5.8
I know how to change the timestamp of a regular file:
touch -t 201301291810 myfile.txt
I was not able to do the same with a symlink. Is it possible?
Distro: RHEL 5.8
add switch -h
touch -h -t 201301291810 myfile.txt
Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create do not create any files
-d, --date=STRING parse STRING and use it instead of current time
-f (ignored)
-h, --no-dereference affect each symbolic link instead of any referenced
file (useful only on systems that can change the
timestamps of a symlink)
-m change only the modification time
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
You may need a more recent version of touch. If this is not an option, and if you know C, you could write a small program to do it yourself using the lutimes function.
A brute force way is as follows:
0. 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.
The atime and mtime of a symbolic link can be changed using the lutimes function. The following program works for me on MacOSX and Linux to copy both times from an arbitrary file to a symbolic link:
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
int
main(int argc, char **argv)
{
struct timeval times[2];
struct stat info;
int rc;
if (argc != 3) {
fprintf(stderr, "usage: %s source target\n", argv[0]);
return 1;
}
rc = lstat(argv[1], &info);
if (rc != 0) {
fprintf(stderr, "error: cannot stat %s, %s\n", argv[1],
strerror(errno));
return 1;
}
times[0].tv_sec = info.st_atime;
times[0].tv_usec = 0;
times[1].tv_sec = info.st_mtime;
times[1].tv_usec = 0;
rc = lutimes(argv[2], times);
if (rc != 0) {
fprintf(stderr, "error: cannot set times on %s, %s\n", argv[2],
strerror(errno));
return 1;
}
return 0;
}
If you call the compiled file copytime, then the command copytime file link can be used to make link have the same atime and mtime as file does. It shouldn't be too difficult to modify the program to use times specified on the command line instead of copying the times from another file.