is one of those commands that I was introduced to rather flippantly. Just as a
consequence of following along with some blog.
is a CLI utility that comes standard on most Unix-based OSes and like the name
simply “touches” i.e creates files without requiring the user to directly
modify them. There is more to
than simply making some quick example files for a tutorial as we will soon
Basic touch usage
touchoperates as follows:
touch -options filetocreate.txt
To create a new file, simply run the following:
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:
Advanced touch Usage
You might be under the impression that
only creates files but the primary purpose of
touch is for timestamp manipulation! We can update the access and modification time
on a file, this is the equivalent of passing
(modification), if the file exists this will
the contents of the file. The timestamp will be updated to the current time.
touch filetotouch.txt # or touch -am filetotouch.txt
-toption 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
touch -t 1711241430 filetotouch2.txt #or 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,
can be a handy tool. Now our empty files have a purpose!
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
inotifywaitto monitor it then open up a
tmuxsection so we can see the monitor output side-by-side.
touchto 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
touch, a simple yet powerful tool that has broader uses than at first glance. From creating empty files to modifying timestamps,
touchcan be incredibly useful in any users’ command wheelhouse. Thanks for reading now go out there and