Introduction
In the Linux world, the commandtouchis a simple but powerful tool that is part of the basic set of shell utilities. Although his name suggests only «touch» a file, its main functionality is double: create empty files when they do not exist and update the time marks (last access and last modification date) of existing files. This behavior makes it a frequent ally for system administrators, developers and anyone who works with scripts and automation.
Basic syntax
The simplest way to usetouchis:
touch [opciones] nombre_del_archivo
If the specified file is not present in the file system,touchIt creates it as an empty file of zero bytes. If it already exists, the command updates its timstamps to the present time, unless otherwise indicated by specific options.
Create Empty Files
One of the most common uses oftouchis to quickly generate test files or placeholders. For example, to create several empty log files in a directory, just:
touch app.log error.log debug.log
This command will produce three files, each with zero size, ready to be filled by application or later processes. In addition, it can be combined with key expansion to create sequences:
touch archivo_{1..5}.txt
The above generates file _ 1.txt, file _ 2.txt,..., file _ 5.txt.
Update dates and hours
When the objective is only to modify the time mark without changing the content,touchHe does it by default. This is useful in scenarios such as:
- Forging the recombination of make-based projects, where source files must look more new than objects.
- Indicate that a configuration file has been revised, even if its content has not changed.
- Reset the timer of certain demons that act according to the age of a state file.
If you want to set a specific date and time instead of the current one, you use the option-tfollowed by a timestamp in format [[CC] YY] MMDDhhmm [.ss]. For example:
touch -t 202312011030.00 informe.pdf
This command sets the time mark ofinforme.pdfDecember 1, 2023 at 10: 30.
Useful options
Some commontouchinclude:
-a: changes only the last-access time (atime).-m: changes only the last modification time (mtime).-c: does not create the file if it does not exist; only updates existing file timstamps.-r referencia: uses the time marks of the reference file as a model.-d cadena: interprets a human-readable chain (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
Here are some commands that illustrate the versatility oftouch:
touch README.md- create an empty file called README.md.touch -a datos.csv- updates only the last-access time of datos.csv.touch -r plantilla.txt copia.txt- copy the time marks from planta.txt to copia.txt.touch -d "2 days ago" notas.txt- set the date of notas.txt two days ago.
Good practices and advice
AlthoughtouchIt is simple, it is appropriate to follow certain recommendations:
- Always check the full or relative path to avoid creating files in unwanted locations.
- In production environments, prefer the option
-cwhen you just want to update timstamps without risk of accidentally creating files. - Documenting in scripts comments the reason behind each use of
touch, since its effect can be subtle but significant for tools such asmakeorcron. - Use ISO 8601 format (
YYYY-MM-DDTHH:MM:SS) with the option-dwhere legibility and accuracy are needed.
Conclusion
The commandtouchmay seem insignificant at first sight, but its ability to create empty files and manipulate time marks makes it an essential element of any Linux user's arsenal. Domain your options and understand when to apply it allows you to optimize development, system management and automation tasks. The next time you need a placeholder file or you want to force a tymprint update, remember thattouchHe's ready to do it with a single line.


