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.

Some Useful Ways To Use The Touch Command In Linux

In today’s article, we’re going to learn some useful ways to use the touch command. It’s not a complete listing of options, it’s just the commands that I think you’re most likely to find useful when using said touch command. This won’t be all that difficult to process, even for a new Linux user.

It pains me to not give this a witty, clickbait title. Though, it does remind me of a story. I think I’ll share it…

So, I was on the Interstate. At the time, my weekend was partially filled up by playing in a Top-40 band. That explains why I had pop radio on the FM radio we all used back then. I was driving along and on came this song for the first time, or at least my first time hearing it.

Yes, the Divinyls song, I Touch Myself. I laughed so hard that I had to pull off to the breakdown lane. I didn’t even notice the cop pull up until he flashed his lights and used his cruiser’s bullhorn thing to tell me that it was an emergency lane and to move on. He didn’t even check if I had an emergency, he just told me to move on. Maybe he too was listening to pop radio at the time?

So, I just want you to know how hard it was for me to *not* use a ‘witty’ article title. You’re welcome. As I often say, I’m pretty much just a giant 5 year old.

Anyhow, like I said in the beginning, this is going to be some useful ways to use the touch command in Linux. Read on!

The Touch Command:

The touch command is a terminal only thing, so you’ll need an open terminal to follow this article. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

As I said, this isn’t going to be a complete listing of touch commands, it’s just the most useful – or those I think you’ll find the most useful. If you want a complete list, just run man touch in your terminal and all shall be revealed.

So, you’re mostly going to use the touch command to create files. You can create files with touch by using the following:

Or create multiple files with:

I suspect many of you know about that, but there’s more to touch! Quite a bit more, actually. It’s a handy commend for other things, like mucking about with timestamps.

Want to create a file – but not make the file if the filename already exists (handy for scripting)? You can do that. The command would be:

Want to change a file’s modification and access time? You can do that with the -a flag, which will change both to the current date and time:

If you want to change just the modification time, use the -m flag.

Now, you can also specify a specific access and modification time, so that you’re not just setting it to the current date and time. It’s not too complicated. It looks like this:

That’s YEAR, MONTH, DAY, HOUR, MINUTE, and SECOND. So, a fairly universal time and date format, something you may know from other commands.

You can also just tell touch to use the timestamp from another file. That’s done with:

In the above case, the command will set the timestamp of <file_1> to that of <file_2>. Pretty handy, huh? 

Again, there’s more to the touch command. To learn more, just type man touch into your terminal and you’ll see the various other options. These are just a few of the options that I’ve used, those that I think are the most useful. It’s a great way to expose newer users to some of the nifty terminal commands available in their Linux systems.

Closure:

And there you  have it. You now have an article that helps you learn how to use the touch command in Linux. It’s a handy command and it does more than just make files. We’ve actually used the touch command in exercises before, so it’s nice to see what other options are available. Also, as tempting as it was, I didn’t use any puns or innuendo in the article. You’re welcome!

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 own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

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.