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
touch command operates as follows:
touch -options filetocreate.txt
To create a new file, run the following:
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:
But enough of the mundane stuff!
Advanced Touch In Linux Usage
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 -am filetotouch.txt
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
touch -t 1711241430 filetotouch2.txt
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.
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.
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!