How to Use the Linux touch Command to Create Files and Change Timestamps

In Linux, every file has metadata timestamps recording exactly when it was last accessed or modified. Usually, these timestamps update automatically when you open a file in a text editor or run a script. However, system administrators and developers often need to manipulate these timestamps manually—for example, to trick a backup script into thinking an old file was just created, or to force a code compiler (like make) to recompile a source file without actually changing its contents. The Linux touch command is the standard utility for manually updating file timestamps, and it also serves as the fastest way to instantly create brand new, empty files from the terminal.

Creating New Empty Files

The most common everyday use of the touch command is quickly generating placeholder files without needing to open a text editor like Nano or Vim.

  1. Open your terminal application.
  2. Type the command followed by the name of the new file you want to create: touch new_script.sh
  3. Press Enter.

The command will execute silently. If you run ls -l, you will see that new_script.sh has been created with a file size of 0 bytes, and its creation/modification timestamps will match the exact second you pressed Enter.

You can also create multiple files simultaneously by listing them separated by spaces:

touch index.html style.css script.js

Updating Timestamps on Existing Files

If you run the touch command against a file that already exists, it will not delete or overwrite the file’s contents. Instead, it simply updates the file’s access and modification timestamps to the current system time.

touch old_database_dump.sql

If the file was originally downloaded in 2021, running this command will make the file appear to the operating system as if it was modified today.

Changing Timestamps to a Specific Date

If you want to backdate a file or set its timestamp to a very specific moment in the past (or future), you can use the -t (timestamp) flag. This requires you to provide the date in a strict numerical format: YYYYMMDDhhmm.ss (Year, Month, Day, Hour, Minute, dot, Second).

For example, to change a file’s modification time to December 25th, 2023, at 09:30 AM:

touch -t 202312250930.00 my_document.txt

Preventing File Creation (Update Only)

If you are writing a script that should only update the timestamp of a log file if that log file already exists, but should not accidentally create a new blank log file if it is missing, you must use the -c (no-create) flag.

touch -c server_error.log

If server_error.log exists, its timestamp updates. If it does not exist, touch fails silently and does nothing.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.