Change timestamp on a file

touch -amct [[CC]YY]MMDDhhmm[.ss] FILE
-a for access time, -m for modification time, -c do not create any files, -t timestamp

1
By: sharfah
2009-05-27 14:33:22

These Might Interest You

  • tstouch takes two arguments: a filename containing a timestamp, and an extended regular expression with the parenthesized section matching a timestamp of the form YYYYMMDDhhmm or YYYYMMDDhhmm.ss. It then touches the file with that timestamp. Show Sample Output


    2
    tstouch() { [[ $1 =~ $2 ]] && touch -t ${BASH_REMATCH[1]} $1; }
    bartonski · 2013-10-01 20:00:34 0
  • History usually only gives the command number and the command. This will add a timestamp to the history file. Note: this will only put the correct timestamp on commands used after the export is done. You may want to put this in your .bashrc Show Sample Output


    33
    export HISTTIMEFORMAT="%F %T "
    fritz_monroe · 2009-05-12 14:36:42 9

  • 8
    touch --date "2010-01-05" /tmp/filename
    raulyca · 2011-11-16 17:07:59 0
  • If you have a logfile where some lines start with timestamps like "2014-05-01 12:34:56,123" but other lines are missing the timestamp (like stack traces or object dumps), then use this script to copy the most recent timestamp to any lines that are missing it. This is useful for merging log files, since you can then safely sort by timestamp to merge the files. Show Sample Output


    0
    perl -ne 'if (/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} )/ ) { $t=$1; print $_ } else { print $t . $_ }'
    unixmonkey76059 · 2014-05-21 08:54:27 0

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: