31

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

  • 2
    What is the problem you are trying to solve? – mdpc Feb 6 '13 at 17:45
  • 2
    But why....what more global problem are you trying to address? Is this just asthetics, or does it have a real purpose? – mdpc Feb 6 '13 at 17:57
  • 7
    that is irrelevant. i am not going into my business logic – amphibient Feb 6 '13 at 18:16
  • 5
    This type of information helps us all to get to a solution that would work for you. Its not irreleveant. Sorry you are so sensitive, I'm just trying to help. – mdpc Feb 6 '13 at 18:30
  • 4
    dude, it is irrelevant. just go with the need for changing the timestamp as a given constant, invariable. you can question it all you want but it is not changing on my end. which makes the questioning pretty much useless, effectively. good luck – amphibient Feb 6 '13 at 18:33
45

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
  • > touch -h -t 201301291810 mysymlink -> touch: invalid option -- h Try `touch --help' for more information. – amphibient Feb 6 '13 at 15:42
  • 2
    look at the quote "useful only on systems that can change the timestamps of a symlink". – mdpc Feb 6 '13 at 17:44
  • 3
    It was also only added recently (it's not in this manpage from 2010). Maybe he needs to just get the latest version of coreutils. Here's the diff of when it was added in 2009 – Random832 Feb 6 '13 at 17:49
  • If this is the correct answer, please mark it as such. – qodeninja Aug 18 at 20:58
  • @qodeninja After over six years, I don't really expect the OP to mark it one way or the other. – Stephan Aug 19 at 21:02
3

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.

0

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.
  • made me curious, what system requires this? btw, any file created while you have not yet fixed the sytem date will have that timestamp too – Aquarius Power Jun 3 '14 at 21:49
  • Because one is unable to modify the symlink inode once created. – mdpc Jun 3 '14 at 22:20
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.