Technical skill

Linux command: touch

Subject: .../LINUX

Create a placeholder or update a file's timestamp

A build or backup program may look at a file's timestamp, a recorded time associated with it, to decide whether to do work. touch creates an empty file when the path does not exist. If the file already exists, it keeps the contents and updates its timestamps. This is useful for making a placeholder or marking a file as changed for another tool.

With no timestamp options, touch report.txt sets its access and modification times to now. If the path is missing, it creates an empty file. touch -c report.txt leaves a missing path absent, and one command can create several names, such as touch cache-a cache-b cache-c.

What the timestamps mean

The access time, or atime, records file access when the filesystem policy updates it. The modification time, or mtime, records a change to file contents. The change time, or ctime, records a change to file metadata; it is not a creation time and touch cannot set it to an arbitrary value. Timestamp precision and access-time updates depend on filesystem policy.

Use -a to change only the access time and -m to change only the modification time; without either, both change. -r reference-file target-file copies timestamps from one path to another. GNU Coreutils also accepts a readable date with -d; check the installed manual for supported formats. POSIX -t uses a compact timestamp format that differs from GNU's more flexible date parsing.

Check what a timestamp change affects

stat report.txt displays timestamp metadata. Build tools and backup programs often compare modification times when deciding which files need attention, so touching a source can trigger a rebuild even if its contents are unchanged. A synchronization job may also treat the updated file as new.

touch does not empty an existing file. Shell redirection such as > report.txt truncates that regular file instead. Use touch when the contents should remain. If a name begins with a hyphen, write it as ./-draft or use an option terminator supported by that implementation.

On Linux, touch normally follows a symbolic link and updates its target. GNU's -h option can update the link itself on systems that support link timestamps. Check the local manual before relying on that behavior in a portable script. Setting an older time can make a file look stale to a release or backup workflow, so verify the target and resulting metadata when other tools depend on those timestamps.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng SRE preview
image descriptionSkyeng SRE
by Yuliya Marochkina
12 users
Skillset Skyeng Senior PlatOps preview
image descriptionSkyeng Senior PlatOps
by Artem Naumenko (slack2)
7 users
Skillset Skyeng Ops (for DevOps) preview
image descriptionSkyeng Ops (for DevOps)
by Roman Burnashev
2 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful