Setting the Timestamp on UNIX files

I recently removed a file in UNIX accidentally – I was running commands from my command history and thought I was after the second most recent command, rather than the third most recent command.

Luckily, I’d kept a copy of the file on my Windows system, so all I wanted to do was to put the file back. As expected, though, when I put the new file over, it took the UNIX timestamp rather than the Windows one. One of the first things that I check for when comparing a development and production environment, if something works in one environment and not in the other, is the timestamps on the two files – if they are the same then the chances are that the files are identical. This isn’t always true, but I’ve always found it a good starter for 10.

So, now I have a file in UNIX that is dated today, when it should have been date three months ago, and I wanted to change the timestamp. In Windows, I’ve got a utility that I can enter the new timestamp, and it updates the file information – in UNIX there is a standard command to do it!

If you run the touch command, you can provide a timestamp that it puts against the file, so all I needed was to run

touch -t 200703062257 [filename]

and the system set the timestamp on the file to 06-MAR-2007 at 22:57 – exactly what I wanted to hide my error in deleting files!

This is probably old-hat to someone who knows UNIX well, but it saved me having to remember the dates that I messed up files, and at least now I have somewhere I can always search to remind me how it’s done!!

By continuing to use the site, you agree to the use of cookies. more information

In common with almost all professionally run websites, this website logs the IP address of each visitor in order to keep it running reliably. This is also essential for protecting the website and its visitors from malicious attacks, including infection with malware.

This website provides information as a service to visitors such as yourself, and to do this reliably and efficiently, it sometimes places small amounts of information on your computer or device (e.g. mobile phone). This includes small files known as cookies. The cookies stored by this website cannot be used to identify you personally.

We use cookies to understand what pages and information visitors find useful, and to detect problems such as broken links, or pages which are taking a long time to load.

We sometimes use cookies to remember a choice you make on one page, when you have moved to another page if that information can be used to make the website work better. For example:
- avoiding the need to ask for the same information several times during a session (e.g. when filling in forms), or
- remembering that you have logged in, so that you don’t have to re-enter your username and password on every page.

You can prevent the setting of cookies by adjusting the settings on your browser (see your browser Help for how to do this). Be aware that disabling cookies will affect the functionality of this and many other websites that you visit.

Close