9

Is it possible to manually alter the date of a file on a UNIX system (to a previous date)?

If so, how would I tackle this problem?

How would I be able to do the same thing on multiple files at the same time?

4
  • 1
    You can use touch file to make it have the current date. – fedorqui Jun 6 '13 at 9:38
  • Updated the question. I'm only looking at making the files look older than they are. – ukliviu Jun 6 '13 at 9:39
  • 1
    You can fake it changing your datetime sudo date -s "sometime in the past", then touch it and go back to current date. – fedorqui Jun 6 '13 at 9:40
  • 1
    Forget my explanation, @BeniBela answer solves this easier. Did not know about -d in touch. – fedorqui Jun 6 '13 at 9:42
18

You can use touch. E.g::

touch -d '2007-01-31 8:46:26' file

Or often easier, if you have a file2 which has already the mtime, you can copy the time with -r:

touch -r file2 file

There is also the -t option with its strange format:

touch -t [[CC]YY]MMDDhhmm[.ss] file
3
  • 1
    +1 since this is a good solution for a Linux box. But it looks like -d is not a valid option on an OS X machine. – ukliviu Jun 6 '13 at 9:46
  • Added an older option which also seems to exist on mac – BeniBela Jun 6 '13 at 9:59
  • 1
    -t is POSIX so it's a good bet to be supported on most current unices. – Wumpus Q. Wumbley Jun 6 '13 at 10:02
1

You can use

 touch -m -d '1 Jan 2006 12:34' test.txt

-m Change only the modification time -d (--date=STRING) with the date you want to put in

extracted from the manual:

DATE STRING
       The  --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday".  A
       date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers.  An empty  string  indicates
       the beginning of the day.  The date string format is more complex than is easily documented here but is fully described in the info documentation.

for more info you can read the touch manual, running man touch.

i hope that helps.

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.