touch is a command to modify the date of filesystem metadata for a given file. By defalt it updates the create date of the given files.

Examples

Create an empty file

touch somefile

Update a file's modify time to a specific date

  • -m to set modify time
  • -t to specify a timestamp in the format [[CC]YY]MMDDhhmm[.SS]
touch -m -t 199902030405.06 1999-02-03-04-05-06.gpx

Update access time on several files

Not all filesystems support access time.

touch -a -t 199509150102 GARBAGE/FILE\ [1234]

Randomize the mtime for a given file

bash's random only goes up to 32767, which is about 9 hours in 1-second increments. With RANDOM * 32767 + RANDOM we can get this up to just over 34 years.

randomize-mtime() {
  seconds="$(( $(date +%s) - $(( RANDOM * 32767 )) - RANDOM))"
  new_mtime="$(gdate -d @"${seconds}" "+%Y%m%d%H%M.%S")"
  echo "${new_mtime} $*" 1>&2
  touch -m -t "${new_mtime}" "$@"
}

# change mtime of all files to the same random mtime
randomize-mtime test-foo{1..3} ;

# change mtime of each file to a different random mtime
for F in test-bar{1..3} ; do
  randomize-mtime "$F"
done