Change a File to Any Desired Time
touch -t
Keep in mind that you aren't constrained to the current date and time. Instead, you can pick whatever date and time you'd like, as long as you use this option and pattern:
-t [[CC]YY]MMDDhhmm[.ss]
. The pattern is explained in Table 2.4.
Table 2.4. Patterns for Changing a File's Times
|
Characters
|
Meaning
|
|
CC
|
First two characters of a four-digit year
|
|
YY
|
Two-digit year:
|
|
|
|
|
|
|
|
|
|
|
MM
|
Month (0112)
|
|
DD
|
Day (0131)
|
|
hh
|
Hour
(0123)
|
|
mm
|
Minute (0059)
|
|
ss
|
Second (0059)
|
It's very important that you include the zeroes if the number you want to use isn't normally two digits or your pattern won't work. Here are a few examples of
touch
with the
-t
option in action to help get you started.
$
ls -l
-rw-r--r-- 239 2005-10-19 14:00 wireless.log
$
touch -t 197002160701 wireless.log
$
ls -l
-rw-r--r-- 239 1970-02-16 07:01 wireless.log
$
touch -t 9212310000 wireless.log
$ls -l
-rw-r--r-- 239 1992-12-31 00:00 wireless.log
$
touch -t 3405170234 wireless.log
$ ls -l
-rw-r--r-- 239 2034-05-17 02:34 wireless.log
$
touch -t 10191703 wireless.log
$ls -l
-rw-r--r-- 239 2005-10-19 17:03 wireless.log
First you establish that the current date and time for
wireless.log
is
2005-10-19 14:00
. Then you go back in time some 35
years
, to
1970-02-16 07:01
, and then forward a little more than 20 years to
1992-12-31 00:00
, and then leap way into the future to
2034-05-17 02:34
, when Linux computers will rule the world and
humans
will live in
peace
and
open
-source
prosperity
, and then finish back in our day and time.
You should draw a couple of lessons from this demonstration. You go back more than three decades by specifying the complete four-digit year (1970), the month (02), the day (16), the hour (07), and the minute (01). You don't need to specify seconds. After that, you never specify a four-digit year again. 92 in
9212310000
is within the range of 6999, so
touch
assumes you mean 19 as the base century, while 34 in
3405170234
lies between 00 and 68, so 20 is used as the base. The last time
touch
is used, a year isn't specified at all, just a month (10), a day (19), an hour (17), and minutes (03), so
touch
knows
you mean the current year, 2005. By understanding how to manipulate
touch
, you can change the date stamps of files when necessary.
|