What Is The Linux TOUCH Command And How To Master It

Table of Contents

The Linux touch command is one of those commands that I gave little regard for when I got introduced to it. I only learned it as a consequence of following along with with some random post on some tech blog. touch is a command line utility that comes standard on most UNIX operating systems. As the name would imply the command “touches” files. It does this without requiring the user to manual open a text editor and create the file. Touch in Linux has more uses than making some quick example files as we will soon see.

Linux Touch Command Usage

The touch command operates as follows:

				
					touch -options filetocreate.txt
				
			

To create a new file, run the following:

				
					touch filetotouch.txt
				
			

With that the touch command creates an 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 get done quicker using curly braces:

				
					touch filetotouch{1..5}.txt
				
			

But enough of the mundane stuff!

Advanced Touch In Linux Usage

The touch command creates files, but what else does it do? According to the man page, the primary purpose of touch is for timestamp manipulation! Shocking I know. To think such a harmless tool gets used to change the date and time of a file. We can update the access and modification times of a file by calling touch on an existing file. This is the same as passing the touch command options: -a (access) and -m (modification). This will not overwrite the file contents. The file timestamp gets updated to the current time. This avoids creating a situation where you could lose data. When pointed at files that exist the touch command will assume timestamp operation.

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

There are way more options available than I am providing here. You can get more detail about them by running this hilarious command: man touch.

We can also update a files’ metadata to have a specific date or time of the file by using the -t option. We can then input 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 include the last 2 digits in the year. Or omit the year and use the current year, and finally the seconds may also get omitted by dropping [.ss].

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

Of course, creating empty files is only so useful, how can we use this command in a meaningful way? Debugging! We can use the touch command to trigger file events or to work in tandem with scripts. Now our empty files have a purpose! Of course updating the modification times of a file or playing a game of set the timestamp is only so useful.

Practical touch

In this next section we will be using inotifywait. For the sake of brevity I won’t be covering that command in detail. Check out Boyd’s post covering the command here. It can get installed with your package manager as inotify-tools. After you have it installed and you wish to follow along, make a sandbox directory. Tell inotifywait to watch it. Then open up a tmux pane so we can see the inotifywait do its thing in one window and enter the command in the other.

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

				
					touch testfile.txt
				
			

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

A simple scenario, but it illustrates the point.

A touching end

That was the touch in linux command, a simple yet powerful tool that has broader uses than at first glance. From creating placeholders to modifying timestamps. Or whatever you could think of that involves creating a file via the command line. touch can be a great addition to any users’ command wheelhouse. Thanks for reading now go out there and touch some files!

Meet the Author

Leave a Reply