The touch command in Linux is primarily used to create empty files or update the timestamps (access and modification times) of existing files. It’s a versatile tool for managing files in scripts, workflows, or system administration tasks.
If the specified file doesn’t exist, touch creates it as an empty file. If the file already exists, touch updates its timestamps to the current time (or a specified time) without altering its content. This makes it useful for creating placeholder files, refreshing timestamps, or ensuring files are up-to-date in automated processes.
The command is simple yet powerful, offering options to control timestamps, avoid file creation, and handle special cases like symbolic links.
Practical Use Cases for the Touch Command
- Refreshing file timestamps to prevent automatic deletion by cleanup scripts
- Creating empty placeholder files as part of application deployment
- Testing file permissions by attempting to touch files in different directories
- Triggering file watchers in development environments
- Initializing log files before an application starts
- Creating empty files as flags/markers for scripts and automation
Create a Single File
touch file.txt
Creates an empty file.txt
if it doesn’t exist. If it does, updates its timestamp.
Create Multiple Files
touch file1.txt file2.txt file3.txt
Creates three empty files or updates existing ones.
Update Timestamps Without Modifying Content
touch existing_file.txt
Updates the access and modification times of existing_file.txt
to the current time.
Set a Specific Timestamp
touch -t 202410231030.00 file.txt
Sets the timestamp to October 23, 2024, 10:30:00 (format: [[CC]YY]MMDDhhmm[.ss]
).
Update Only Access Time
touch -a file.txt
Updates the access time (leaves the modification time unchanged).
Update Only Modification Time
touch -m file.txt
Updates the modification time (leaves the access time unchanged).
Create a Hidden File
touch .hiddenfile
Creates a hidden file (starts with .
).
Avoid Creating New Files
touch -c non_existing_file.txt
-c
(no-create) updates timestamps only if the file exists.- Does not create
non_existing_file.txt
if missing.
Sync Timestamps with Another File
touch -r reference_file.txt target_file.txt
Copies the timestamp from reference_file.txt
to target_file.txt
.
Set Timestamp Using a String
touch -d "2023-01-15 08:30:00" file.txt
Sets the timestamp to a human-readable date/time.
Create Files with Spaces/Special Characters
touch "my report.txt" # Quotes for spaces
touch file\ name.txt # Backslash to escape spaces
touch 'file$name.txt' # Quotes for special characters
Create a Directory of Files
touch dir/{file1,file2,file3}.txt
Creates file1.txt
, file2.txt
, and file3.txt
inside dir/
.
Bulk Create Sequentially Named Files
touch file-{1..10}.txt
Creates file-1.txt
through file-10.txt
.
Update Symbolic Link Timestamps
touch -h symlink
-h
updates the symlink’s timestamp, not the target file’s.
Force Timestamp in the Past
touch -d "2 days ago" file.txt
Sets the timestamp to 48 hours ago.
Use in Scripts to Create Placeholder Files
touch /tmp/process.lock
Creates a lock file to indicate a running process.
Create Empty Log Files with Dates
touch "log_$(date +%F).txt"
Creates log_2023-10-23.txt
(uses the current date).
Reset All Timestamps to Current Time
touch -am file.txt
-a
updates access time.-m
updates modification time.- Together, they reset both timestamps to “now.”
Key Notes:
- Timestamps: Use
stat file.txt
to verify access/modification times. - Permissions: Requires write access to update timestamps of existing files.
- Safety:
touch
does not modify file content (only metadata).
The touch
command is indispensable for scripting, logging, and file management. 🐧