Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
1  
What is the problem you are trying to solve? –  mdpc Feb 6 '13 at 17:45
    
i need to alter a timestamp so the symlink looks like it was created in the past –  amphibient Feb 6 '13 at 17:52
1  
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
    
that is irrelevant. i am not going into my business logic –  amphibient Feb 6 '13 at 18:16
2  
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 Answers 4

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
share|improve this answer
    
> touch -h -t 201301291810 mysymlink -> touch: invalid option -- h Try `touch --help' for more information. –  amphibient Feb 6 '13 at 15:42
1  
look at the quote "useful only on systems that can change the timestamps of a symlink". –  mdpc Feb 6 '13 at 17:44
2  
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

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.

share|improve this answer

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|improve this answer
    
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 at 21:49
    
Because one is unable to modify the symlink inode once created. –  mdpc Jun 3 at 22:20

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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