Touch Command on Linux: Tutorial and Examples

Touch Command on Linux: Tutorial and Examples

In this tutorial, we’re going to show you what the touch command is, how to use it, and include practical examples of using the command.

Unlike other commands that you should never run on Linux, the touch command is actually recommended and often used by everyone on Linux.

What’s the touch command on Linux?

The touch command is used in Unix systems (like Linux distros) to create new files or update the timestamps of existing files via the CLI. When using the touch command (unlike the cat command), the newly created file will be empty. Although the primary function of the touch command is to update the timestamps of files, it’s also often used to create files.

There are many useful examples and practical use-cases of this command, we’ll go through them below.

How to use the touch command on Linux?

The syntax of the touch command is:

touch [OPTION] [FILE]

If you wanted to create an empty file called “day1.txt”, the most basic example is:

touch day1.txt

If the file already exists, the touch command will update its timestamps to the time of running that command.

We’ll explore other options and use cases in detail below.

Touch command options

Here are all the options/flags you can use with the touch command:

  • -a: change the access time only
  • -c: do not create any files
  • -d, –date=STRING: parse STRING and use it instead of the current time
  • -h: affects each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)
  • -m: change the modification time only
  • r=FILE, –reference=FILE: use this FILE’s timestamp instead of the current time
  • -t STAMP: use STAMP, [[CC]YY]MMDDhhmm[.ss], instead of the current time
  • –help: shows you a help text (with more options than this)

There are other options too, but these are the most useful. You can get all the options by using the –help option:

touch --help

Or by checking the man page:

man touch

Real world, useful, practical examples of the touch command in Linux

Here are some examples of the touch command that you can use in real use-cases. You’ll better understand and learn the touch command through these examples.

Aside from the simple example of how to create a file in the section above, here are other examples of the touch command:

Create multiple files

You can use the touch command to create multiple files at once. Each file name should be separated by a space. The syntax is:

touch filename1 filename2 file3

A useful example is when you need to create pages for a website:

touch index.html about-us.html plugins.php

Another way to create multiple files is by creating a batch of files with a single command. The file names will be similar, with the numbers added in brackets that will be added to the end of each file name. The syntax is:

touch filename{1..10}

A useful example is if you needed to create a diary/report of sorts for each day of the month:

touch day{1..31}.txt

This example will create the files: day1.txt, day2.txt, day3.txt…all the way to day31.txt.

The same command can also be used with letters. A useful example is if you need to create files with names/words that start with a certain letter. Example:

touch keywordswith{a..z}.csv

This will create the files: keywordswitha.csv, keywordswithb.csv, keywordswithc.csv, all the way to keywordswithz.csv.

You can’t combine numbers and letters with the {X..Y} syntax.

Change timestamps of files

To change/set the timestamp of a file, use the -t option.

The syntax is:

touch -t FILENAME

“YYYY” is the year – example: 2021. “MM” is the month – ex: 04. “DD” is the day – ex: 30. “hh” are the hours – ex: 05. “mm” are the minutes – ex: 49. And “ss” is the seconds – ex: 51.

So, if you’d wanted to set the timestamps of “work-report.txt” to 05:49:51(time) 04.30.2021(date), you’d run this command:

touch -t 202104300549.51 work-report.txt

You can verify the changes by using the “stat” command:

stat work-report.txt

Which should output, among other data, the Access and Modify times:

Access: 2021-04-30 05:49:51.000000000 +0300
Modify: 2021-04-30 05:49:51.000000000 +0300

If you don’t want to update the Access or Modify times, you can either use the “-a” option to modify the Access time only, or the “-m” option to modify the Modify time only. The syntax is the same as when using the “-t” option.

If you want to update the access time to current/now, just use the -a option:

touch -a work-report.txt

It’s the same if you’d use the “-m” option. So the real-world use case is that you’d use the “-a” option when accessing the file, and once you’re done modifying it, use the “-m” option. You can also combine the two options and use “-am”

You can combine the “-t” option with both the “-m” and “-a” options. So if you’d like to update the modification time to a specific date and time, you can use:

touch -mt 202104300549.51 work-report.txt

If the date/time format above is confusing to you, you can use a string for the date. It’s pretty flexible on what you can use. The syntax is:

touch -d STRING work-report.txt

Acceptable strings for dates are:

  • Times of the day like 5:49am
  • Days like “Monday”, “Tuesday”, etc.
  • Calendar dates like 30 April 2021.
  • You can even use strings like “tomorrow”, “yesterday”, “next monday”, “5 years ago” etc.

As an example, if you wanted to set the timestamps to tomorrow for the file “todo.txt”, you’d run this command:

touch -d tomorrow todo.txt

If you wanted to change the timestamps of “CHANGELOG” to 5 years ago, you’d run:

touch -d "5 years ago" CHANGELOG

If you want to set the timestamps to “current”, just run the -t option:

touch -t CHANGELOG

You can reference another file to set the timestamp of a file. The syntax is:


So, if you’d like to use changelog.old’s timestamps to, you’d use:

touch -r changelog.old

There are other examples and use cases of the touch command, but we only included those that we thought are the most useful. If you have any examples or suggestions for the touch command on Linux, leave a comment below.

Leave a comment

Your email address will not be published.