Home Beginner's Guide 9 ways to use the Touch command with examples

9 ways to use the Touch command with examples

by Divya Kiran Kumar
touch command examples

Have you ever wondered what the touch command does? If you did, you have come to the right place.  It is a versatile command that not only lets you create new files but also lets you change the timestamps of an existing file.

In this tutorial, we shall show you touch command usage with examples to help you understand better.

Touch command usage for beginners with examples

We are using the latest Ubuntu release 19.04 for the tutorial. The tutorial should work fine for those who are using the lower version until 16.04.

1) Create a new file

The touch command is handy when it comes to creating a new file. The syntax of creating a new file is as below:

$ touch filename

Let’s see the command in action below.

$ touch learningtouch

Creating files is a common task in Linux. You should have used the cat command to create a new file. However, the touch command works differently. In the latter case, it should only create the file and do nothing more. In the case of the cat command, the user can instantly populate it with content.

In short, you create an empty file using the touch command.

creating-empty-file-using-touch

Creating an empty file using the touch command

2) Create multiple files

You can also create multiple files; all you need to do is add the filename(s) after the command separated by space. The syntax is as below.

$ touch filename1 filename2 filename3

Let’s see it in action.

$ touch file1 file2 file3
creating-multiple-files-touch

Creating multiple files using the touch command

3) Force touch to not create a file

You can make a touch command not to create a file. To do so, you need to use -c option along with the command. The syntax is as below.

$ touch -c filename
$ touch -c pleaseletmelive
force-touch-not-create-file

Force touch command to not create a file

If you try to access “pleaseletmelive” file, you should get the following error

cannot access 'pleaseletmelive': No such file or directory

4) Change file access time

The touch command is also handy when it comes to changing the access time. To do so, you need to use the operator -a.

$ touch -a filename

Let’s see it in action.

$ touch -a learning touch

After that, run the stat command to see the access time:

$ stat learningtouch
changing-access-touch

Changing access using -a

5) Change the modified time

You can also change the modified time using the -m option.

$ touch -m filename
$ touch -m learningtouch
changing-modified-time-touch

Changing file modified time using -m touch option

6) Changing modified time and access time together

The -am option enables you to change both the modified and access time of the file.

$ touch -am filename
$ touch -am learningtouch
Changing-modified-access-time-together

Changing the access and modified time of a file using the -am option

7) Setting a specific time

If you want to mention a specific time, then you can do it by using the -t option. It is also advisable to use -c option along with it so that it doesn’t create a file if it doesn’t exist.

The time should be specified in the following format

[[CC]YY]MMDDhhmm[.ss]
$ touch -c -t [[CC]YY]MMDDhhmm[.ss] filename

Let’s see it in action.

$ touch -c -t 202001052335 learningtouch

In the above command, we changed the time to the year 2020, 5th Jan, 23:35

set-specific-time

Set specific time using the -t option

8) Change timestamp in reference to another file

The touch command can also be used to change timestamps based on another file.

$ touch -r referencefile filename

Let’s see it in action.

changing-timestamp-reference-touch

Changing a file timestamp using another file

9) Using DateTime as a string

Lastly, you can also use a string specifying the date to which you want to set the file timestamp.

$ touch -c -d "DD MMM" filename
$ touch -c -d "3 Jan" learningtouch
set-time-date-using-string

Changing time and date using string input

Conclusion

This leads us to the end of our touch commands for beginners. If you found them useful, then comment below!

You may also like

1 comment

pobiddy January 19, 2021 - 8:40 PM

Great article, and well written. Thank you. I don’t understand an application for NOT creating a file. Why just not create one – why use the touch command.
I have a few “dummy” password databases that I keep as decoys. I use the touch -d command in Anacron to update the access and modify times on these each day so they appear to be legitimate, and so my authentic database doesn’t stand out.
Cheers,
pobiddy

Reply

Leave a Comment

fl_logo_v3_footer
ENHANCE YOUR LINUX EXPERIENCE.

FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews, FOSS Linux is the go-to source for all things Linux. Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY

ALL RIGHTS RESERVED.