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.
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:
touch foo
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:
touch -a foo
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:
rm foo
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:
touch -t <year><month><day><24 hour><minutes>.<seconds>
Or, something like this:
touch -t 202303010001.01 foo
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:
touch -r bar foo
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:
rm foo
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.
Today we'll cover one way to enable or disable your network interface in the Linux…
Today's exercise is a nice and simple exercise where we check your NIC speed in…
Have you ever wanted to easily monitor your wireless connection? Well, now you can learn…
I think I've covered this before with the ls command but this time we'll count…
Today we'll be learning about a basic Linux command that's known as 'uname' and it…
If you've used hardinfo in the past, it may interest you to know that hardinfo…