The touch command in Linux is used to alter the access and modification times for a given file or set of files. It’s also a super handy way to create an empty file very quickly, if you are running the touch command and specify a file that doesn’t already exist.
Many users will find themselves using the touch command every once in a while, or maybe even quite often. When working with a Linux operating system, sometimes a need arises to create an empty file. Some service, which an administrator is configuring, requires a certain file to be created before the service can start and, sometimes, this file may need to have certain access time stamp.
Other scenarios where touch proves useful is in applications that sort files according to their modification time. If you want to manipulate this sorting, without actually editing any files, touch can be used to update the modification time with any date and time that you supply. In this guide, you’ll learn how to use the touch command in Linux through examples. Follow along below to see learn about the various options that you can use with this command.
In this tutorial you will learn:
- How to use the touch command on Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
The touch command has a simple syntax and a very simple purpose, so it doesn’t take long to learn. The following examples will teach you everything you need to know about the touch command, and the advanced section will show you some lesser used options that may come in handy every once in a while.
touch command in Linux Basic Examples
- As mentioned earlier, one of the primary uses of touch is to create new, empty files. This is probably the quickest and easiest way to generate a new file on Linux, and one that system administrators will find themselves using often. The syntax is very simple – just specify the file name you wish to create.
$ touch document.txt
- Create any number of files by just specifying more file names in the command.
$ touch file1 file2 file3
- If the file you specify with touch already exists, the command will update its modification time. Let’s look at an example where we check the modification time of a file with the stat command, then run the touch command on the file and check its modification time again. You’ll see that the modification time has been updated to the system’s current date and time.
$ stat example.txt Access: 2021-08-10 00:08:47.660120649 -0400 Modify: 2021-08-10 00:08:43.936129005 -0400 Change: 2021-08-10 00:08:43.936129005 -0400
We have cut some of the excess output here. But as you can see, the current access time of the file is 2021-08-10 00:08:47 and the modification time is 2021-08-10 00:08:43. Now let’s run the touch command and then check again.
$ touch example.txt $ stat example.txt Access: 2021-08-16 22:59:27.315203013 -0400 Modify: 2021-08-16 22:59:27.315203013 -0400 Change: 2021-08-16 22:59:27.315203013 -0400
As you can see, the atime, mtime, and ctime have all been changed with one simple touch command.
- The default behavior of the touch command is to change the file’s times to the current date and time. But it’s possible to specify any date and time that we want by using the
-toption. For example, let’s change our
example.txtfile to have a modification time from some random date like January 15, 2019 at 03:25 AM. Feel free to adapt this date and put something more creative. The syntax for the date is
$ touch -t 201901150325 example.txt
Verify the change with stat.
$ stat example.txt Access: 2019-01-15 03:25:00.000000000 -0500 Modify: 2019-01-15 03:25:00.000000000 -0500
- If you’d only like to change the access time of a file, rather than the modification time, use the
-aoption in your touch command.
$ touch -a example.txt
- Conversely, the
-moption can be used with the touch command to only update a file’s modification time.
$ touch -m example.txt
You can always use the man command to read more about the touch command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
The touch command is pretty simple, but it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know of them. However, they can definitely come in handy in various situations. In this section of the guide, we’ll show you a few of the lesser known options of the touch command that we think are useful.
touch command in Linux Advanced Examples
- Use the
-roption with the touch command to specify a reference file. What this does is change a file’s modification time to match the modification time of a different file that you specify. After executing the command, both files will have the same modification time.
$ touch -r file example.txt
In that example, our
example.txtfile is given the same modification time as
- If you want the touch command to update modification times but avoid creating new files if they don’t already exist, add the
--no-createoption to your command.
$ touch -c file1 file2 file3
- Use the
--no-dereferenceoption to instruct touch to update the modification time of a symbolic link instead of the file it references.
$ touch -h example.txt
--dateoption can be used to specify a custom date for the file’s modification time, just like the
-doption we covered earlier. However, the
--dateoption allows us to specify a human readable string, rather than the
YYYYMMDDhhmm[.ss]format. Here’s how it looks.
$ touch --date="Sun, 29 Feb 2004 16:21:42 -0800" example.txt OR $ touch --date="2019-01-15 03:25:00" example.txt OR $ touch --date="next Friday" example.txt
In this guide, we learned all about the touch command on Linux. The touch command proves very useful when you need to create a new, empty file or set files, and it’s the easiest way to modify the modification time or access time of any file. The command may not make it into your daily repertoire, but you’ll find that it comes in handy more often than you would think.