Changing the timestamp of a symlink

34,090

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.
Share:
34,090

Related videos on Youtube

amphibient
Author by

amphibient

Software Engineer with table manners

Updated on September 18, 2022

Comments

  • amphibient
    amphibient over 1 year

    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

    • text
      text about 11 years
      What is the problem you are trying to solve?
    • text
      text about 11 years
      But why....what more global problem are you trying to address? Is this just asthetics, or does it have a real purpose?
    • amphibient
      amphibient about 11 years
      that is irrelevant. i am not going into my business logic
    • text
      text about 11 years
      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.
    • amphibient
      amphibient about 11 years
      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
    • chipimix
      chipimix over 2 years
      @mdpc, I have the same problem, I want to tune the order, which torrents are listed in my Transmission GUI.
  • amphibient
    amphibient about 11 years
    > touch -h -t 201301291810 mysymlink -> touch: invalid option -- h Try `touch --help' for more information.
  • text
    text about 11 years
    look at the quote "useful only on systems that can change the timestamps of a symlink".
  • Aquarius Power
    Aquarius Power almost 10 years
    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
  • text
    text almost 10 years
    Because one is unable to modify the symlink inode once created.
  • qodeninja
    qodeninja over 4 years
    If this is the correct answer, please mark it as such.
  • Stephan
    Stephan over 4 years
    @qodeninja After over six years, I don't really expect the OP to mark it one way or the other.
  • Mikko Rantalainen
    Mikko Rantalainen about 3 years
    @mdpc See lutimes().