Let’s Play With Touch And Time

Today’s article is just a fun article where we get the chance to play with touch and time. This article won’t necessarily be something you rely on daily, but it should at least be informative. That and there’s surely a subset of users who use these commands. There’s bound to be! Otherwise, why would they make touch flags for adjusting the time?!?

We’ve used the ‘touch’ command before.

Some Useful Ways To Use The Touch Command In Linux

I’m pretty sure we’ve also used the touch command in at least another article. There are a lot of search results for the word and it’s not important enough to go looking. The key point is that touch is a command you use in the terminal and it describes itself as:

touch – change file timestamps

See, we (and perhaps most other sites) have used the touch command to *make* files, but it’s useful (and intended for) changing file timestamps. Like oh so many commands, there are all sorts of ways to use it and so many folks do things in their own way. The great thing about Linux is that we have so many choices.

This is why I figure I’ll cover touch and time in this article. It’s just a few commands that we’ll be using, so it’s not all that advanced. A beginner probably won’t need to know this sort of information, but it’s information worth sharing.

Playing With Touch And Time:

As we use touch in the terminal, you’re going to need to have an open terminal to play with touch and time. If you don’t know how to open a terminal, press CTRL + ALT + T and your default terminal should open.

With your terminal now open, let’s start by creating a file:

That command will create a file with a created timestamp from the time you ran the command. If you want, you can change the access time, that is the time stored with the file that says when it was last accessed. Run the following command with the -a flag:

That’ll change the ‘foo file’s access time to the time when you ran the command. All well and good? Alright… Let’s remove that file with:

Now, we’re going to create the foo file again, but we’re going to give it a specific timestamp. The format you want is:

Or, something like this:

That will give the file a timestamp from March 3rd at one minute and one second past midnight. Note the period denoting seconds as that’s the only modifier you need for this command.

NOTE: If you use a different time format, that command might be different for you. I don’t think it is, but I’m not set up to test that. If it does matter, please let us know in a comment, thanks!

Finally, let’s say you have a file named ‘bar’ and you want ‘foo’ to have the same timestamp that bar has. You can do that with the touch command. It’s relatively easy to do with the -r flag:

That command will give foo the same timestamp that bar has, should you be inclined to do so. It’s pretty easy to use the touch command to change a timestamp on files, which is why you might not want to rely too much on timestamps. They can be useful, but they are not immutable. 

You can run the command above to delete the foo file when you’re done playing around with the touch command:

Closure:

There you go, another article. This time, we’ve had an article about using touch and time, which is kinda why the touch command exists in the first place. It’s far more useful than just creating files. Be sure to run man touch (no inappropriate jokes) in the terminal to learn more about the touch command. There are other touch commands that you may find yourself interested in, I’ve just covered a few of them.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Subscribe to Newsletter!
Get notified when new articles are published!
We promise to never share your email!
icon

Author: KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.