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 the 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. I 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,318FansLike
385FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

6 Essential Command-Line Utilities Every Linux User Should Know

Last week, we shared with you several "cool and fun" commands to get comfortable and confident with the Linux command-line. In our quest to further aid Linux users with mastery of the command line, or CLI, we present you with a variety of command-line utilities essential for all Linux users, regardless of proficiency level.

What is the difference between Linux and Unix?

When discussing Linux and Unix with average users, it's not uncommon that they will sometimes mistakenly interchange the terms Linux and Unix.  The two are not the same.  Though they share similarities in their overall structures and toolkits, they are decidedly not the same.

5 Things to do when your Linux system GUI freezes

All operating systems freeze at some point and, Linux distributions are no exceptions. In this post, we are going to look at five methods you can apply to your frozen system before thinking of plugging out the power cable or holding down the power button to power off the PC.

The 6 Best Download Managers for Fedora

It is a well-known fact that using download managers can help improve download speeds as compared to web browsers. Apart from the inbuilt download manager wget on Fedora, just as on any distribution that is based on GNU/Linux package, there are more options to explore.