Right, touch. The name is a bit of a misnomer. You’re not gently caressing a file into existence; you’re either giving the filesystem a poke to update a timestamp or, if the file’s not there, conjuring it out of the digital void with the absolute bare minimum of content: zero bytes. It’s the command-line equivalent of saying, “I claim this land… for a future project, maybe.”

Its primary, historical job isn’t even creation—it’s timestamp updating. Back in the day, build systems would use timestamps to figure out what needed recompiling. touch was the tool to trick them into thinking a file had just been modified so the build would proceed. The fact that it creates a file if one doesn’t exist is just a useful side effect.

The Absolute Basics: Making Something from Nothing

The simplest incantation. You want a new, empty file? Just touch it.

touch my_new_file.txt

Boom. It’s there. ls -l will show you a file with a size of 0 and the current date and time as its modification and access timestamps. No editors, no fuss. This is incredibly useful for creating placeholder files, signaling that a process should start (e.g., a “lock” file), or just making sure a directory isn’t empty before you git push.

The Real Reason: Timestamp Jujitsu

This is where touch earns its keep. Let’s say you have a file, important_document.txt, and you’ve just read it. You want to mark it as “accessed” now, or maybe you need to set its modification time to a specific date for… reasons (don’t ask, sometimes you just have to).

# Update the access and modification times to right now (the default behavior)
touch important_document.txt

# Now look at the timestamps
ls -l important_document.txt

The most common flags you’ll use are -a (change only the access time) and -m (change only the modification time). But here’s the first “gotcha”: by default, if you don’t specify either, touch updates both timestamps to the current time. It’s an all-or-nothing deal unless you tell it otherwise.

Forcing a Specific Time

Sometimes, you don’t want “now.” You need to set the timestamp to a specific date, perhaps to fix a backup or to make it look like you finished that report last week. Enter the -t flag. Its format is… a choice. [[CC]YY]MMDDhhmm[.ss]. Yes, really.

Want to set a file’s timestamp to January 15th, 2023, at 3:30 PM? You’d write:

touch -t 202301151530.00 my_file.txt

It’s clunky, but it works. A slightly more human-friendly option is -d or --date, which accepts a wider range of date strings (though its support can vary slightly between systems).

touch -d "last Friday" my_file.txt
touch -d "2023-01-15 15:30:00" my_file.txt

The Subtle Pitfall: Referencing and Timestamps

Here’s a classic head-scratcher that gets everyone eventually. What happens if you touch a file that’s symlinked?

# Create a file and a symlink to it
echo "content" > original_file
ln -s original_file my_link

# Now touch the link
touch my_link

Did you update the timestamp on the link itself? Nope. You updated the timestamp on the target file, original_file. This is almost always what you want. The link is just a pointer; touching the pointer should affect what it points to. If you really want to update the symlink’s own metadata (a rare need), you have to use the -h flag. This is a perfect example of the system’s logic: it defaults to the useful behavior 99% of the time and hides the weird one behind a flag.

Best Practices and the “No-Clobber” Safety Net

Let’s say you’re writing a script that uses touch to create a log file. You run it. It works. You run it again. It works again. But what if someone has, in the meantime, replaced your empty log file with a crucial system configuration? Your script would blindly touch it and erase its contents, reducing it to a zero-byte file. Catastrophe.

This is why you should almost always use the -c (or --no-create) flag in scripts. It tells touch: “Only update the timestamps if this file already exists. If it doesn’t, do nothing. Don’t create it.”

# Safe for scripts: Only update timestamp if the file exists.
touch -c potential_log_file.txt

It prevents you from accidentally destroying existing files. It’s a simple habit that separates the cautious from the soon-to-be-panicked.

So, to sum up: use touch to create empty files quickly, but remember its heart is in manipulating time. Use -c in scripts to avoid disaster, and don’t be surprised when it follows symlinks to their target. It’s a simple tool, but like everything on the command line, the devil—and the power—is in the details.