The Linux Crash Course series teaches you an important Linux command or concept in every video, and you can watch each episode in any order. This time around, Jay explains the touch command.
Basic Usage
To begin, let’s take a look at basic usage of the touch command. But first, we need to make sure that we have the touch command available to us in the first place. To check that, we can run the following command:
command -v touch
For the vast majority of you, the touch command will be installed automatically as part of your distribution. However, if you receive no output from that command, that means touch isn’t installed. If you do need to install it, all you should need to do is install the coreutils package. Most distributions should have this available, and all you should have to do is run a command similar to this to install it:
sudo apt install coreutils
If you’re using a distribution that utilizes dnf instead of apt, you can run the following instead:
sudo dnf install coreutils
And that’s all there is to it when it comes to gaining access to the touch command.
But once you have it, how do you use it? Well, what I’ll do right now is go over its basic usage and then we’ll expand from there.
To start, here’s the current list of files within my home directory:
ls -l
Take a quick note of the files and directories I have listed here.
Anyway, the first example I’ll give you is going to be the most common that you’ll use with this command.
touch hello-world.txt
Notice that I received no output, but if I check my file listing again, you’ll see that hello-world.txt has been created and its size is zero. Literally – it’s an empty file.
Like I mentioned, this is going to be a common example with the touch command. If you use the touch command and provide it with a filename, it will create an empty file. And you might be wondering why you’d want to create an empty file. To be fair, it’s not going to be something you’ll do every day, but you might end up in a situation where a file needs to be present with a specific file name. One example of this is with a service that works with one or more log files. Sometimes, you’ll find yourself moving a log file to another location, such as moving it to an archive directory for safe-keeping. Most of the time, if a service finds that its log file is missing, it will attempt to create it automatically. However, some services will outright fail if its log file isn’t present, and the touch command is the fastest way to ensure that a file exists. It won’t contain any content, but it’ll exist.
And this leads right into one of the most important things to keep in mind when it comes to the touch command. We just saw an example of using it to create an empty file, but if a file already exists – then the behavior of the touch command will be different. To see this in action, here’s my file listing again:
ls -l
At this point, pay special attention to the modification time shown next to the text file we created earlier. Let’s run the exact same command again that we ran last time:
touch hello-world.txt
Then, let’s view our file listing again:
ls -l
As you can see, the modification time has changed. And this change in behavior is important to keep in mind.
So, here’s what happens. When you use the touch command and provide it with a filename, it will create an empty file if it doesn’t already exist. However, if the file does exist when you run the command, it will update the modification time. As you saw, the behavior of the touch command changes depending on whether the file you reference already exists or not.
The thing is though, it’s not going to be super common that you’ll need to update the modification time of a file, but sometimes you’ll find a situation where it makes sense. One example has to do with backing up files. If you run a backup program or script to copy files to a backup server, it’s often the case that the utility you’re using won’t back up the file again if it’s already done that. But maybe you want the file to be backed up a second time, regardless of whether or not it’s been changed. If you use the touch command against it, then any backup utility that grabs files based on modification time will see that the modification time has changed – which would result in the utility backing up the file again.
Anyway, another benefit of being able to easily create empty files is when you’re planning for a project. Perhaps you want to build file structure ahead of time, so you can see what your project directory might look like. Maybe you haven’t gathered all of the files you’ll need, but if you already know what they are, you can use the touch command to create empty files as a sort of place-holder. Then, once you have the file structure set up, you can begin the process of creating the files you’ll end up needing one by one.
If you think you might be needing a larger number of files, you can actually instruct the touch command to create more than one at a time – rather than running the touch command for each one by one. So, consider this example:
touch hello-world-1.txt touch hello-world-2.txt touch hello-world-3.txt touch hello-world-4.txt
In this example, the touch command will make sure each of those files exist, and if any of them do exist already, it’ll also update the modification time accordingly.
Whenever you do use the touch command against an existing file, it’ll update the modification time to reflect the date and time as of when you ran the command. If you’re curious what the current date and time is, you can run the date command:
date
And that’ll give you an idea of whether or not your system is set to the correct date and time before you start using the touch command.
Speaking of the date, another trick with the touch command is that you can instruct it to use a specific date and time for the modification time, rather than whatever the clock happens to show currently:
touch -t 202507221200 file.txt
Before I run this command, let’s be sure we understand the date string here. It’s probably obvious to at least some of you, but what we have here is a date string that’s formatted to include the year/month/date/hour and minutes. You can change this to the particular date you’re looking for.
So, as you can see, you can update the modification time on a file with the touch command. However, you should take care when adjusting modification times. Sometimes, it might result in negative behavior. For example, by changing the modification time of a file you might trick a backup utility into thinking that it doesn’t need to be backed up, because it’s showing an older date now.
But not only that, changing a file’s modification time might even be a policy violation. For example, if your company ever becomes the victim of a cyber attack, checking when files were modified is part of the process of investigating the breach. If you change the modification time on files, it might mislead security people that are looking for a list of files that were modified between specific times. Just be mindful that sometimes modification times play a role in various aspects of system administration, and you should only do this if you have a particular reason to do so. However, since we’re creating brand-new files within this tutorial, this won’t make a difference.
Anyway, what I’ll do right now is show you some additional options with the touch command. But first, I want to point out something I haven’t mentioned yet. The touch command works on directories too. This means that if you do need to change the modification time of a directory, you can do that with the touch command – and it’s done the exact same way. The touch command is smart enough to understand the difference between files and directories.
With that out of the way, let’s change the logic of the touch command a bit. Earlier in the article, I mentioned it will create a file if it’s not already present, but if it does exist – it’ll update the modification time. What we’re going to do this time is change the logic such that it won’t create a new file, but if a file does exist it will still update the modification time:
ls -l
touch -c hello-again.txt
ls -l
In this case, the file I referenced didn’t already exist, so the touch command did nothing. That’s because the -c option prevents it from creating new files, but it will still update the modification time if the file does happen to be present.
So, I’ll create the file this time:
touch hello-again.txt
And then I’ll use the touch command again with the -c option:
touch -c hello-again.txt
Now, of all the examples I’ve shown you, the -c option is probably something you’ll use the least. However, it might be a good idea to include it in your notes, as you never know – you might need it some day.
Let’s see another example, and this time I’ll show you the -r option. What this enables you to do is use the time stamp of another file. And this one might come in handy when backing up files. For example, let’s say you created a very important file last Tuesday and you want to back it up so you won’t lose it. So, you decide to copy it to a flash drive or backup server. If you check the timestamp for the backed up file, it might show today’s date – even though it wasn’t actually created today, you were just backing up the existing file. By using the -r option, you can use the timestamp from the original file, and apply it to the file you’ve backed up. Here’s how it works:
First, we decide which file we want to use as a reference.
Then, we’ll choose the file we want to receive the timestamp.
And to carry out the process, we’ll construct the touch command similar to this:
touch -r reference.txt target.txt
Now, you’ll see that the timestamp of those two files matches:
ls -l
And that’s about it, the command was simple but it’s still important to know…


