Master the Linux ‘touch’ Command: A Comprehensive Guide

Peter Hou
2 min readApr 20, 2023

--

This document provides a comprehensive guide to the Linux touch command, including its history, usage, common parameters, and tricks and tips. The touch command is primarily used to create new, empty files or update the timestamps of existing files, and can be used for various purposes. The document also includes common use cases and tricky skills, as well as notes on how to use the command correctly.

Instructions

In this article, you will learn about the Linux touch command, its history, when and why to use it, its usage, common parameters, and some tricks and tips to get the most out of it.

History

The touch command was developed as a part of Unix operating systems to easily create or update the access and modification timestamps of files.

When and why to use it

touch is primarily used to create new, empty files or update the timestamps of existing files. This can be useful for various purposes, such as creating a temporary placeholder, marking a file as recently accessed, or forcing a file to be rebuilt.

How to use it

To create a new file or update the timestamp of an existing file, simply type touch followed by the file name:

touch myfile.txt

The commonly used parameters

  • -a: Update only the access time of the file.
touch -a myfile.txt
  • -m: Update only the modification time of the file.
touch -m myfile.txt
  • -t: Set the access and modification times to a specific time, specified as [[CC]YY]MMDDhhmm[.ss].
touch -t 202204112359 myfile.txt
  • -r: Use the access and modification times of a reference file.
touch -r reference.txt myfile.txt
  • -c: Do not create a new file if it does not already exist.
touch -c myfile.txt

Other supported parameters

  • -h: Update the timestamps of a symbolic link, not the file it points to.

Most common use cases

touch is most commonly used to:

  1. Create new, empty files.
  2. Update the access and/or modification times of existing files.
  3. Set timestamps to specific values or match those of another file.
touch newfile.txt
touch -t 202204112359 existingfile.txt
touch -r reference.txt anotherfile.txt

The tricky skills

  • Create multiple files at once by specifying multiple file names:
touch file1.txt file2.txt file3.txt
  • Set a file’s timestamps in the future or past to simulate different scenarios:
touch -t 202304112359 futurefile.txt

What needs to be noted

  • When using the -t option, ensure the timestamp format is correct, or the command may not work as expected.
  • The -c option can be helpful to avoid accidentally creating unwanted files.

Conclusion

The touch command is a simple yet versatile tool in Linux systems for creating and managing file timestamps. By understanding its usage and various parameters, you can make the most of this command to efficiently manage files on your system.

--

--

Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.