Linux Phrasebook
Authors: Granneman S.
Published year: 2007
Pages: 38-39/288
Buy this book on amazon.com >>


Change a File to the Current Time

touch

The touch command isn't one you'll find yourself using constantly, but you'll need it as you proceed through this book, so it's a good one to cover now. Interestingly, the main reason for the existence of touch to update the access and modification times of a fileisn't the main reason you'll be using the command. Instead, you're going to rely on its secondary purpose that undoubtedly gets more use than the primary purpose!

Note

You can only use the touch command on a file and change the times if you have write permission for that file. Otherwise, touch fails.


To simultaneously update both the access and modification times for a file (or folder), just run the basic touch command.

$

ls -l ~/

drwxr-xr-x     848 2005-10-19 11:36 src
drwxr-xr-x    1664 2005-10-18 12:07 todo
drwxr-xr-x     632 2005-10-18 12:25 videos
-rw-r--r--     239 2005-09-10 23:12 wireless.log
$

touch wireless.log

$

ls -l ~/

drwxr-xr-x     848 2005-10-19 11:36 src
drwxr-xr-x    1664 2005-10-18 12:07 todo
drwxr-xr-x     632 2005-10-18 12:25 videos
-rw-r--r--     239 2005-10-19 14:00 wireless.log


Thanks to touch , both the modification time and the access time for the wireless.log file have changed, although ls -l only shows the modification time. The file hadn't been used in more than a month, but touch now updates it, making it look like it was just ... touched.

You can be more specific, if you'd like. If you want to change just the access time, use the -a option (or --time=access ); to alter the modification time only, use -m (or --time=modify ).



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:

 
  • If 0068, assumes that first two digits are 20

 
  • If 6999, assumes that first two digits are 19

 
  • If nothing, assumes current 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.


Linux Phrasebook
Authors: Granneman S.
Published year: 2007
Pages: 38-39/288
Buy this book on amazon.com >>

Similar books on Amazon