Linux / Command / touch / Creating empty files and changing last modification time

In Linux, touch creates an empty file if it does not exist, and if the file exists, it updates the access and modification times to the current time by default. It does not erase the content of existing files, but it can affect backups or build operations that rely on time information.

Creating empty files

The following command creates an empty file in the current directory if example.txt does not exist. Subsequent examples are based on touch and stat from GNU coreutils. Run them in a separate empty practice directory to avoid affecting existing data.

touch example.txt
ls -l example.txt

A file with size 0 will appear in the list. If a file with the same name already exists, it does not overwrite it with a new empty file but updates the existing file's time. If the name contains spaces, enclose it in quotes like touch "my notes.txt". If multiple names are listed separated by spaces, each will be processed.

Checking and Updating the Time of an Existing File

The access time (atime) of a file relates to the last access, and the modification time (mtime) relates to the last modification of the file content. By default, touch example.txt sets both times to the current time. You can check before and after the change as follows.

stat example.txt
touch example.txt
stat example.txt

Compare the Access and Modify entries in the output of stat. The actual output language and display format may vary depending on the environment, and differences at very short intervals may not be visible due to the time resolution of the file system.

The status change time (ctime) is related to changes in metadata such as permissions and is not the same as the creation time. Changing the time information with touch can also update the ctime, but you cannot set ctime to an arbitrary value or change the creation time. Whether the access time is updated on every read is also affected by the file system and mount settings. You can check the meaning of each time in GNU file time description.

Changing Only the Access Time or Modification Time

-a selects only the access time, and -m selects only the modification time. Below is an example of changing the modification time of a practice file to the current time and checking the result.

touch -m example.txt
stat example.txt

-a selects the access time, while -m selects the modification time. It does not mean that the file’s status change time will be preserved. If the file does not exist, an empty file is created unless a separate option is specified.

Use a desired time or the time from another file

You can specify the date and time with GNU touch -d. The example below sets the access and modification times of the practice file to the specified time.

touch -d '2024-01-15 10:30:00' example.txt
stat example.txt

If the time zone is not specified separately, it will be interpreted according to the environment variable TZ or the system time zone. Verify that the output access and modification times match the specified values. The saved values may vary depending on the file system's supported range and resolution.

To use another file as a reference, use -r. The following is an example when both reference.txt and example.txt exist in the current directory.

touch -r reference.txt example.txt
stat reference.txt example.txt

Set the target file’s access and modification times to the values of the reference file. If the reference file does not exist, an error occurs, and by default, an empty file may be created if the target does not exist. For detailed options, refer to the GNU touch manual.

Update only the time without creating a file

-c does not create a new file if the specified file does not exist. The following command updates the time to the current time if the file already exists, and does not create it if it does not exist.

touch -c example.txt

Since it can finish without error even when the file does not exist, do not use only the success status of this command to check the existence of the file. To create a new file, write permission is required for the parent directory, and the permission to change the time of an existing file depends on the owner and the time you want to set. If an error occurs, check the path and permissions.