Home » Linux » Applications » Linux Bash Touch

touch Command in Linux and Bash [with Examples]

The touch command in Linux updates the timestamps on a file or creates the file if it doesn’t exist. See some examples and use cases below.

It sounds useless, but it’s actually useful. For example, if you want to create an empty file called my_file.txt, you can just run:

touch my_file.txt

Easy!

Updating the timestamps of a file is also useful.

Say you have a file called favorite_tv.txt, which you use to keep the name of your current favorite TV show.

Your favorite show 10 years ago was The Flintstones, and it still is. It hasn’t changed. But the timestamp for this file shows it was last edited 10 years ago. Anyone looking at that file might not know it’s still your favorite show.

By running:

touch favourite_tv.txt

… the contents of the file would remain unaltered, but anyone looking at the timestamps would see that The Flintstones is still your favorite show, as the modification timestamp is recent.

touch Syntax

touch [OPTIONS] file1 [file2 file3...]

Note that:

  • One or more file paths can be supplied
  • Optional OPTIONS can be supplied – see the below table for some common ones
  • By default, touch will update both the access and modification times of the listed file

Options

-a change only the access time
-c Do not create any files
-m Change only the modification time
-d DATE Use DATE instead of current time where DATE is a date/time in a parsable string
-h Affect each symbolic link instead of any referenced file

For more options and details, you can view the touch manual by running:

man touch

touch Examples

Creating a new file or updating the access and modification timestamps is already covered above – here are some more examples using the above options.

To confirm the changes made by any of the below examples, you can run:

stat <file-name>

Changing Only Access Time

touch -a my_file.txt

Changing Only Modification Time

touch -m my_file.txt

Setting a Specific Date/Time

touch -d '17 March 2021 10:26' my_file.txt

Symbolic Links

By default, touch will operate on the file referenced by a symbolic link rather than on the link itself. To change this and update the timestamps of the link, use the -h option:

touch -h my_symbolic_link
SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment