What Is The TOUCH Command And How To Use It In Linux

Table of Contents

touch is one of those commands that I was introduced to rather flippantly. Just as a consequence of following along with some blog. touch is a CLI utility that comes standard on most Unix-based OSes and like the name implies; touch simply “touches” i.e creates files without requiring the user to directly modify them. There is more to touch than simply making some quick example files for a tutorial as we will soon see.

Basic touch usage

touch operates as follows:
					touch -options filetocreate.txt

To create a new file, simply run the following:

					touch filetotouch.txt

Just like that we have a brand new empty file! We can also create multiple files at once by separating the filenames with spaces:

					touch filetotouch.txt filetotouch2.txt filetotouch3.txt

This can also be done more effectively using curly braces:

					touch filetotouch{1..5}.txt

Advanced touch Usage

You might be under the impression that touch only creates files but the primary purpose of touch is for timestamp manipulation! We can update the access and modification time by calling touch on a file, this is the equivalent of passing -a (access) and -m (modification), if the file exists this will not overwrite the contents of the file. The timestamp will be updated to the current time.

					touch filetotouch.txt
# or
touch -am filetotouch.txt

Obviously there are way more options available than I am providing here, but you can get more detail about them by running this hilarious command: man touch.

We can also update a file’s timestamp to have a specific date by using the -t option and inputting the date we want in this format: [[CC]YY]MMDDhhmm[.ss]. Below we are setting the date for Oct 31, 2017 at 12:30:56.
					touch -t 201710311230.56 filetotouch.txt
We can verify the timestamp change with ls -la:

Note that in the time formatting above that the values in the square brackets are optional. For example we can omit the century, and just include the last 2 digits in the year or omit the year entirely and just use the current year, and finally the seconds can also be omitted

					touch -t 1711241430 filetotouch2.txt
touch -t 11241430 filetotouch3.txt

Of course, creating empty files is only so useful, how can we use touch in a meaningful way? Debugging! From working with scripts, to triggering file events for an app or simply creating placeholders for the future, touch
can be a handy tool. Now our empty files have a purpose!

Practical touch

In this next section we will be using inotifywait. For the sake of brevity I won’t be covering the command in detail just yet, but it can be installed with your package manager as inotify-tools. After that I’ll make a sandbox directory and tell inotifywait to monitor it then open up a tmux section so we can see the monitor output side-by-side.

If you need a tmux refresher check out our article here.

					touch testfile.txt
We can then switch to a 2nd pane, navigate into the sandbox and use touch to create a dummy file that will trigger inotifywait. We have now confirmed that we are monitoring the directory correctly.

A simple scenario, but it effectively illustrates the point.

A touching end

That was touch, a simple yet powerful tool that has broader uses than at first glance. From creating empty files to modifying timestamps, touch can be incredibly useful in any users’ command wheelhouse. Thanks for reading now go out there and touch some files!

Meet the Author

Leave a Reply

Your email address will not be published. Required fields are marked *