4

This sounds like an easily researched question. It isn't. I'm following the following highly recommended post on an Ubuntu Stack Exchange site that I happened to run across. But the suggestion doesn't work on Red Hat Enterprise Linux ES release 4. I would have expected it to, but it fails, as detailed below.

This is the suggestion: Specifically, the poster recommends

If you want to modify the file relative to its existing modification time instead, the following should do the trick:

touch -d "$(date -R -r filename) - 2 hours" filename

This does not work for me under Redhat. The minus sign is ignored and the time is set ahead two days just as if I'd entered

touch -d "$(date -R -r filename) + 2 hours" filename

For example:

$ ls -al test
-rw-r-----  1 sc1478 dev 5 Oct 27 12:59 test

$ touch -d "$(date -R -r test) - 8 days" test

$ ls -al test
-rw-r-----  1 sc1478 dev 5 Nov  4  2016 test

$ touch -d "$(date -R -r test) + 8 days" test

$ ls -al test 
-rw-r-----  1 sc1478 dev 5 Nov 12  2016 test

Whether I use a minus sign or a plus sign, the date is adjusted forward.

Is this a bug in some version of touch?

Is there another way to adjust a file's timestamp relative to its current timestamp?

  • Redhat 4 appears to ship with a coreutils 5.2.1 vintage 2004 but from some digging around in the git log under the tag for that version in theory both -r and -d at least exist on touch (commit 7430e918cb579aa0d340e12b7dfe003d7dca6746). – thrig Oct 27 '16 at 20:10
  • Note that Linux filesystems do not have a 'created' timestamp, only 'modified', 'changed', 'accessed'. The 'changed' timestamp refers to the last status change, i.e. when the file metadata like owner or access mode was modified. The 'modified' timestamp is updated when the file is written to or truncated, and 'accessed' when the file is read -- but mount options are often used to suppress 'access' time updates either completely (noatime) or when the 'modified' timestamp is already older (relatime). – Nominal Animal Oct 27 '16 at 20:11
3

Updated Answer

I stumbled across this gem

touch -r filename -d '+8 days' filename

From the info coreutils touch invocation (Thank you @don_crissti ):

'-r FILE'

'--reference=FILE'

 Use the times of the reference FILE instead of the current time.
 If this option is combined with the '--date=TIME' ('-d TIME')
 option, the reference FILE's time is the origin for any relative
 TIMEs given, but is otherwise ignored.  For example, '-r foo -d
 '-5 seconds'' specifies a time stamp equal to five seconds before
 the corresponding time stamp for 'foo'.  If FILE is a symbolic
 link, the reference timestamp is taken from the target of the
 symlink, unless '-h' was also in effect.

If you want variable expansion you can use soft quotes around the -d argument like so.

DAYS=8
touch -r filename -d "+${DAYS} days" filename

Sample:

$ ls -l foo.bar
-rw-r--r-- 1 usr usr 69414810 Nov 10  2016 foo.bar
$ TEST=100
$ touch -r foo.bar -d "+${TEST} days" foo.bar
$ ls -l foo.bar 
-rw-r--r-- 1 usr usr 69414810 Feb 24  2017 foo.bar
  • Didn't try your first suggestion, way too complicated, after seeing your second solution, which works. Thanks. – Steve Cohen Oct 27 '16 at 19:53
  • @SteveCohen yeah, after posting it I thought to my self that there had to be a better way... – Zachary Brady Oct 27 '16 at 19:54
  • Please do explain what it does. – Rui F Ribeiro Oct 27 '16 at 20:03
  • And also how you'd handle it if instead of +8, you had a variable. – Steve Cohen Oct 27 '16 at 20:08
  • @SteveCohen then use soft quotes around the -d argument like so touch -r filename -d "+${MYVAR} day" filename – Zachary Brady Oct 27 '16 at 20:15

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.