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
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
to that of
. 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.
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!