9 ways to use the Touch command with examples

The touch command is handy when it comes to creating a new file. The syntax of creating a new file is $ touch filename. Let's delve into details with 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!

Divya Kiran Kumar
I'm Editor of FOSS Linux. I worked as a Software Engineer, before taking up blogging as my full-time job. I enjoy using Linux, and can't imagine anything else for my PC. Apart from writing for FOSS Linux, I enjoy reading non-fictional books. Sapiens was my favorite last read. Hope you enjoy reading and using this blog to enhance your Linux experience! Have a great day ahead!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,874FansLike
265FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Create a ClamAV Antivirus Live USB drive, and how to use it

Antivirus Live CD or USB drives come in handy at times when your computer is infected with a virus and other malware. Here is how you can create a ClamAV Live USB drive to scan and remove the malware from a computer.
screen capture apps linux
Do you want to make a video tutorial for YouTube or show-off your epic victory royale to your friends? Look no further — as we have compiled the 10 Best Screen Capturing Software for Linux. Whether you need to record your desktop screen or take a simple screenshot, FOSSLinux has got your back.
gamemode enable linux
GameMode is a combination of various libraries and daemons that allows all the users to improve the gaming performance on the Linux system. Developed by games publisher Feral Interactive, it improves gaming performance by requesting a group of options that will be applied temporarily to the Linux system.