The touch command in Linux: creating empty files and updating dates

Introduction

In the Linux world, the touch command is a simple yet powerful tool that is part of the basic set of shell utilities. Although its name suggests only “touching” a file, its primary functionality is twofold: creating empty files when they do not exist and updating the timestamps (last access and last modification dates) of existing files. This behavior makes it a frequent ally for system administrators, developers, and anyone working with scripts and automation.

Basic Syntax

The simplest way to use touch is:

touch [options] filename

If the specified file is not present in the filesystem, touch creates it as a zero‑byte empty file. If it already exists, the command updates its timestamps to the current time, unless otherwise indicated by specific options.

Creating Empty Files

One of the most common uses of touch is to quickly generate test files or placeholders. For example, to create several empty log files in a directory, simply run:

  • touch app.log error.log debug.log

This command will produce three files, each zero‑byte in size, ready to be filled by the application or by subsequent processes. Additionally, you can combine it with brace expansion to create sequences:

  • touch archivo_{1..5}.txt

The above generates archivo_1.txt, archivo_2.txt, …, archivo_5.txt.

Updating Dates and Times

When the goal is solely to modify the timestamp without changing the content, touch does this by default. This is useful in scenarios such as:

  • Forcing recompilation of make‑based projects, where source files must appear newer than the object files.
  • Indicating that a configuration file has been reviewed, even though its content has not changed.
  • Resetting the timer of certain daemons that act based on the age of a state file.

If you wish to set a specific date and time instead of the current one, use the -t option followed by a timestamp in the format [[CC]YY]MMDDhhmm[.ss]. For example:

  • touch -t 202312011030.00 informe.pdf

This command sets the timestamp of informe.pdf to December 1, 2023 at 10:30:00.

Useful Options

Some frequently used options of touch include:

  • -a: change only the last access time (atime).
  • -m: change only the last modification time (mtime).
  • -c: do not create the file if it does not exist; only update timestamps of existing files.
  • -r reference: use the timestamps of the reference file as a template.
  • -d string: interpret a human‑readable date string (e.g., -d "yesterday") to set the date.

These options allow granular control over which aspect of the timestamp is modified and prevent accidental file creation when not desired.

Practical Examples

Below are some commands that illustrate the versatility of touch:

  • touch README.md – creates an empty file named README.md.
  • touch -a datos.csv – updates only the last access time of datos.csv.
  • touch -r plantilla.txt copia.txt – copies the timestamps from plantilla.txt to copia.txt.
  • touch -d "2 days ago" notas.txt – sets the date of notas.txt to two days ago.

Best Practices and Tips

Although touch is simple, it’s wise to follow certain recommendations:

  • Always verify the full or relative path to avoid creating files in unintended locations.
  • In production environments, prefer the -c option when you only want to update timestamps without the risk of accidentally creating files.
  • Document in script comments the reason behind each use of touch, as its effect can be subtle but significant for tools like make or cron.
  • Use the ISO 8601 format (YYYY-MM-DDTHH:MM:SS) with the -d option when readability and precision are needed.

Conclusion

The touch command may seem insignificant at first glance, but its ability to create empty files and manipulate timestamps makes it an essential tool in any Linux user’s arsenal. Mastering its options and knowing when to apply it allows you to optimize development tasks, system administration, and automation. The next time you need a placeholder file or want to force a timestamp update, remember that touch is ready to do it with a single line.

This post is also available in ESPAÑOL.

Leave a Reply

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

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .