The touch command in Linux creates empty files and updates file timestamps (access and modification times) without altering content. It’s essential for scripting, build systems, and server maintenance. This user guide explains touch syntax, options (-a, -m, -c, -d, -t, -r), practical examples, common errors, and best practices for production Linux servers.
If you’re new to Linux or refining your shell skills, mastering the touch command in Linux pays off immediately. It lets you create files on the fly, standardize timestamps, and trigger time-based workflows—vital on web servers, CI/CD pipelines, and development machines. This beginner-friendly guide covers real-world usage with clear examples and pitfalls to avoid.
What is the touch command in Linux?
touch is a standard POSIX utility that updates a file’s timestamps. By default, it sets both access time (atime) and modification time (mtime) to “now,” creating the file if it doesn’t exist. With options, you can choose which time to update, copy timestamps from another file, or set specific dates for reproducible builds and audit trails.
Touch command syntax and core options
touch [OPTION]... FILE...
Common GNU/Linux options (coreutils):
- -a: Change only the access time (atime)
- -m: Change only the modification time (mtime)
- -c or –no-create: Do not create files if they don’t exist
- -d “STRING”: Parse a date/time string (flexible formats)
- -t [[CC]YY]MMDDhhmm[.ss]: Set a specific timestamp using a strict numeric format
- -r FILE: Use FILE’s times instead of current time
Note about portability: -d is GNU-specific (Linux). On BSD/macOS, use -t or -r. Some flags differ across platforms—details below.
Create files and update timestamps (basics)
Create an empty file
touch app.log
If app.log doesn’t exist, touch creates it as an empty file. If it exists, touch updates its atime and mtime to the current time.
Create multiple files at once
touch index.html style.css app.js
touch logs/{api,auth,db}.log
Leverage brace expansion to create structured file sets quickly—useful when scaffolding projects or pre-creating log files.
Update only atime or only mtime
# Update only access time
touch -a report.csv
# Update only modification time
touch -m report.csv
This is handy for tests or tools sensitive to mtime versus atime. For example, backup systems may use mtime deltas; update only what you need.
Do not create a file if it’s missing
touch -c data/archive.tar.gz
-c (or –no-create) ensures you only touch existing files, which is safer in production scripts where accidental creation could mask errors.
Set specific timestamps with -t and -d
Using -t with a numeric timestamp (POSIX-friendly)
# Format: [[CC]YY]MMDDhhmm[.ss]
touch -t 202401311045.30 invoice.pdf # 2024-01-31 10:45:30
-t works on GNU, BSD, and macOS. It’s ideal when you need portability across environments, including build servers and CI/CD.
Using -d with a human-friendly date (GNU/Linux only)
touch -d "2024-10-05 14:15:00" release-notes.md
touch -d "next Monday 9am" sprint.txt
touch -d "2023-12-01T07:30:00Z" snapshot.bin
GNU’s -d parser is powerful. It supports ISO 8601, relative times (“yesterday”, “2 weeks ago”), and time zones. Avoid -d when scripting for macOS/BSD targets; use -t instead.
Copy timestamps from another file with -r
touch -r reference.img clone.img
-r is perfect for reproducible builds, artifact normalization, and ensuring consistent times across related files for caching or deployment pipelines.
Practical examples and real-world use cases
Pre-create log files with correct ownership and permissions
sudo install -o www-data -g www-data -m 640 /dev/null /var/log/app/app.log
sudo touch /var/log/app/app.log
install can set ownership and mode atomically, while touch confirms the file exists before service start. This prevents permission errors when daemons can’t create logs under stricter umask or SELinux policies.
Force a rebuild in make or CI
# In Makefiles, touching a target updates mtime
touch .force-rebuild
Build systems like make depend on mtime comparisons. touch is the simplest way to invalidate caches or force recompilation without changing file contents.
Trigger file watchers and sync tools
Many watchers (inotify-based tools, rsync with –update, backup jobs) react to timestamp changes. touch can trigger processing when a source file is logically “new” though unchanged in content.
Apply timestamps recursively to a directory tree
# Update mtime across a tree (GNU find)
find ./static -type f -exec touch -m {} +
touch itself isn’t recursive. Combine with find for bulk operations, e.g., normalizing timestamps before packaging or archiving.
Create files quickly with sequences
touch file{01..10}.txt
Useful for testing scripts and workflows needing multiple sample inputs with predictable names.
Understanding timestamps: atime, mtime, ctime
- atime (access time): Last time a file was read
- mtime (modification time): Last time file content changed
- ctime (change time): Last time metadata changed (permissions/owner/mtime). You cannot set ctime manually; it is updated by the kernel.
touch affects atime and/or mtime. Any touch operation will also update ctime automatically because file metadata changed.
Permissions, ownership, and umask behavior
When creating new files, permissions start from 666 masked by the current umask, and ownership is the invoking user (or set by directory ACLs).
- Typical default creation mode: 664 (rw-rw-r–) on umask 002, or 644 (rw-r–r–) on umask 022
- To control secure defaults in shared environments, tune umask in shell profiles or systemd unit files
- On production servers, consider using install or explicit chmod/chown after touch
GNU vs BSD/macOS differences in touch
- -d is GNU-only. On macOS/BSD, prefer -t or -r.
- -h (affect symlink itself) exists on BSD/macOS. GNU coreutils touch does not support -h; changing symlink times is not generally applicable.
- Date parsing: GNU -d accepts natural language; BSD is stricter.
- Portability tip: For cross-platform scripts, stick to -t and -r, and avoid GNU-only flags.
Common errors and how to fix them
“No such file or directory”
touch won’t create missing parent directories. Ensure the path exists:
mkdir -p /var/www/app/logs
touch /var/www/app/logs/app.log
“Permission denied”
You lack write permission on the target directory or file. Fix by changing ownership/permissions or using sudo where appropriate:
sudo chown www-data:www-data /var/log/app
sudo touch /var/log/app/app.log
“Invalid date format” with -t or -d
- For -t, use the exact numeric format: [[CC]YY]MMDDhhmm[.ss]
- For -d, ensure you’re on GNU/Linux and quote the date string
- Time zones: Use offset or Z in ISO strings (e.g., 2024-10-05T14:15:00+05:30)
Unexpected times due to time zones or DST
Servers may run UTC while your laptop uses local time. Normalize to UTC in scripts:
TZ=UTC touch -d "2024-10-05 12:00:00" release.tar.gz
Filesystems with limited timestamp resolution
On some filesystems (e.g., FAT), time resolution is coarse. Two quick touches may look identical. Add a short sleep in scripts if ordering matters:
touch a && sleep 1 && touch b
Best practices for production and hosting environments
- Be explicit with intent: use -a or -m to avoid unintended changes
- Prefer -t for portability across Linux and macOS build agents
- Use -r to replicate timestamps for reproducible builds and stable cache keys
- Combine find with touch for batch operations, and test with echo before executing
- Guard scripts with set -euo pipefail and validate inputs to prevent destructive mistakes
- Document time zone assumptions; standardize on UTC for teams and servers
Running Linux apps on managed infrastructure like YouStable’s SSD hosting or VPS? Pre-create logs and temp files with touch during deployment hooks to avoid runtime permission errors. Our support team can help you set secure umask defaults and automate file prep in systemd or CI pipelines.
touch vs other ways to create files
- touch file: Creates empty file or updates times; safe and simple
- : > file: Truncates file to zero bytes; beware of data loss if file exists
- printf “” > file: Like truncation; can embed content if needed
- install -m MODE /dev/null file: Create with specific permissions atomically
Use touch when you need to preserve content and only adjust timestamps, or when you simply need to ensure a file exists without truncation.
Scripting patterns with touch
Idempotent deploy markers
touch -c /var/run/app/.deployed # Only if it exists
Use -c to avoid creating stray markers that may confuse automation.
Normalize timestamps for deterministic artifacts
# Set a fixed timestamp before packaging
find build -type f -exec touch -t 202401010000 {} +
Great for reproducible tarballs or containers where timestamp variance breaks checksums.
Security and compliance notes
touch can alter timestamps without changing content. For audit trails, log such actions in CI/CD, restrict who can write to sensitive directories, and rely on immutable storage or append-only logs. Remember: ctime will still update, allowing investigators to detect metadata changes.
FAQs about the touch command in Linux
What does the touch command do if a file already exists?
It updates the file’s atime and mtime to the current time by default, without changing content. With -a or -m, you can target just one timestamp. ctime updates automatically as a result of the metadata change.
How do I create multiple files with touch?
List them or use brace expansion: touch file1 file2 file3 or touch file{01..10}.txt. You can also combine directories and names: touch logs/{api,auth,db}.log.
How do I set a specific timestamp with touch?
Use -t with the numeric format [[CC]YY]MMDDhhmm[.ss], which is portable, or -d “YYYY-MM-DD hh:mm:ss” on GNU/Linux. Example: touch -t 202401311045.30 file.txt.
What’s the difference between touch and : > file?
touch creates a file if missing or updates timestamps if it exists, preserving content. : > file truncates the file to zero bytes, destroying existing content.
Why didn’t touch create my file?
Either the parent directory doesn’t exist, you used -c/–no-create, or you lack write permission. Create the directory (mkdir -p), remove -c, or adjust permissions/ownership.
Can I use touch to change only atime?
Yes, use touch -a file. Note that on some mounts with noatime or relatime, atime behavior may differ to reduce disk writes.
Does touch work on symlinks?
On GNU/Linux, touch follows symlinks and updates the target’s timestamps. BSD/macOS provide -h to affect the link itself; GNU touch generally lacks -h. For portability, avoid modifying symlink metadata directly.
Key takeaways
- Use touch to create empty files and update timestamps safely
- -a and -m target specific timestamps; -c prevents accidental file creation
- -t is portable; -d is GNU-only but flexible
- -r copies timestamps for reproducible builds and stable caching
- Combine with find for bulk operations and standardize on UTC in scripts
Whether you manage a single VPS or a fleet of web servers, touch belongs in your daily toolkit. Need help optimizing deployments or setting secure defaults? YouStable’s hosting experts can help you implement reliable, production-ready Linux workflows.