The touch command in Linux is one of those tools that looks too simple at first glance.However, it becomes an essential part of day-to-day administration in shell scripts, cron jobs, CI/CD pipelines, Docker builds, and log rotation workflows. This article provides detailed information on all aspects of using the touch command in Linux, including practical examples that reflect real-world tasks performed by system administrators.
As its core, touch command performs only two functions; it creates an empty file (or overwrites an existing file), and it updates the timestamp of a file. While these two features may seem to have limited functionality, they give touch incredible flexibility when used in automated or scripted workflow.
Note:
If you need to check file details after using touch, the ls command in Linux gives you a quick listing of files and their timestamps right from the terminal.
Examples
What Is the touch Command?
The touch command is a standard Unix/Linux utility that belongs to the GNU Core Utilities package. Its primary job is to update the access and modification timestamps of a file to the current time. If the file does not exist, touch creates it as an empty file.
This behavior makes it useful in two completely different scenarios. First, when you need a placeholder file quickly without writing any content into it. Second, when a script or build system depends on a file's timestamp to decide whether to reprocess something touch lets you manually trigger that logic.
Every file on a Linux system carries three timestamp values:
- atime - the last time the file was accessed or read
- mtime - the last time the file's content was modified
- ctime - the last time the file's metadata or content changed
touch can update any of these independently, or set them to a custom date and time you define. This level of control is what makes it genuinely useful beyond just "creating blank files." If you are new to Linux, it helps to first get comfortable with basic Linux commands before diving into timestamp management.
Common touch Command Options
LinuxTeck.com
You can pass one or more filenames after the options. When multiple filenames are given, touch processes all of them in sequence. Options let you control which timestamp gets updated, what value it gets set to, and whether to avoid creating a new file if it does not already exist.
Options
| Option | Description |
|---|---|
| -a | Update only the access time (atime) of the file |
| -m | Update only the modification time (mtime) of the file |
| -c, --no-create | Do not create the file if it does not already exist |
| -t STAMP | Use a custom timestamp in the format [[CC]YY]MMDDhhmm[.ss] instead of current time |
| -d STRING, --date=STRING | Parse a human-readable date string and use it as the timestamp |
| -r FILE, --reference=FILE | Copy the timestamp from a reference file instead of using current time |
| -h, --no-dereference | Affect symbolic links themselves rather than the files they point to |
| --time=WORD | Specify which timestamp to update: access, atime, use, mtime, or modify |
| --help | Display help information for the touch command |
| --version | Show version information |
Examples
I. Create a Single Empty File
This is the most basic Linux touch command example. If the file does not exist, touch creates it as an empty file with zero bytes. If it already exists, touch updates its timestamps without changing any content inside it.
LinuxTeck.com
Verify the file was created:
LinuxTeck.com
Tip:
Notice the file size is 0 bytes. touch creates the file entry in the filesystem but writes no data into it. This makes it perfect for placeholder files in scripts and project scaffolding.
II. Create Multiple Files at Once
You can pass several filenames in one command. This is one of the most practical Linux touch command examples for anyone setting up a project structure quickly without writing a separate create command for each file.
LinuxTeck.com
LinuxTeck.com
-rw-r--r-- 1 sysadmin sysadmin 0 May 08 10:25 index.html
-rw-r--r-- 1 sysadmin sysadmin 0 May 08 10:25 README.md
-rw-r--r-- 1 sysadmin sysadmin 0 May 08 10:25 style.css
III. Avoid Creating a New File with the -c Flag
Sometimes you only want to update a timestamp if a file already exists. You do not want touch to accidentally create a file you did not intend to make. The -c flag handles that cleanly.
LinuxTeck.com
LinuxTeck.com
Note:
The -c option is very useful inside automation scripts where you want to update timestamps on existing files only, without the risk of creating unintended empty files in your directory structure.
IV. Update Only the Access Timestamp
The -a flag changes only the atime of a file. This is helpful when you want to simulate a file being read without changing its modification time. Build systems and backup tools often check mtime specifically, so keeping it untouched can matter a lot in automated workflows.
LinuxTeck.com
Confirm the access time updated using the --time=atime flag:
LinuxTeck.com
V. Update Only the Modification Timestamp
The -m flag updates only the mtime. This is useful when you want a build tool or deployment script to think a file was recently modified, triggering a rebuild without actually editing any content inside the file.
LinuxTeck.com
Check the full timestamp details with stat to confirm mtime was updated:
LinuxTeck.com
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 131073 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/sysadmin) Gid: ( 1000/sysadmin)
Access: 2026-05-08 10:45:00.000000000 +0530
Modify: 2026-05-08 10:52:00.000000000 +0530
Change: 2026-05-08 10:52:00.000000000 +0530
Birth: -
VI. Set a Custom Timestamp Using -t
The -t flag lets you set a specific timestamp instead of using the current system time. The format is [[CC]YY]MMDDhhmm[.ss] year, month, day, hour, minute, and optionally seconds. This is commonly used in testing, data migration, and log replay scenarios.
Warning:
Setting incorrect timestamps on source files can break Makefile-based build systems. The make tool uses mtime to decide which files need recompiling. Wrong timestamps can cause unnecessary full rebuilds or, worse, skip rebuilds that should have happened.
LinuxTeck.com
LinuxTeck.com
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Access: 2026-01-01 12:00:00.000000000 +0530
Modify: 2026-01-01 12:00:00.000000000 +0530
Change: 2026-05-08 11:00:00.000000000 +0530
Birth: -
VII. Set Timestamp Using a Human-Readable Date String
The -d flag accepts a natural date string that is easier to read and write than the -t format. This is often preferred in scripts where clarity matters more than strict format compliance.
LinuxTeck.com
LinuxTeck.com
VIII. Copy Timestamp from Another File Using -r
The -r flag copies the timestamp from a reference file. This is very useful in backup and restore scripts where you want a restored file to carry the same timestamp as the original, making audits and comparisons straightforward.
LinuxTeck.com
LinuxTeck.com
Modify: 2026-04-10 09:00:00.000000000 +0530
File: restored.conf
Modify: 2026-04-10 09:00:00.000000000 +0530
Tip:
This is especially useful when you restore config files from a backup archive and want them to appear identical to the originals, timestamps included. It can prevent unnecessary config management tool re-runs that trigger on mtime changes.
IX. Bulk File Generation with Brace Expansion
Brace expansion in bash combined with touch is a fast way to generate many files at once. This technique is regularly used when setting up test environments, preparing log rotation directories, or scaffolding a new project layout without writing loops.
LinuxTeck.com
LinuxTeck.com
file6.txt file7.txt file8.txt file9.txt file10.txt
Tip:
For zero-padded filenames used in log rotation setups, use: touch file{01..20}.log this creates file01.log through file20.log, which sort correctly in alphabetical order and work well with log parsers that expect consistent naming.
X. Using touch in Bash Scripting for Lock File Automation
In bash scripting and automation workflows, touch is commonly used to create lock files, flag files, and job status markers. These files signal to other processes whether a job is already running, completed, or failed a simple but reliable coordination mechanism.
LinuxTeck.com
LOCKFILE="/tmp/myjob.lock"
if [ -f "$LOCKFILE" ]; then
echo "Job is already running. Exiting."
exit 1
fi
touch "$LOCKFILE"
echo "Job started at $(date)"
# ... your job logic here ...
rm -f "$LOCKFILE"
echo "Job completed."
Job completed.
Note:
Lock file patterns like this are a standard practice in cron job setups and background service scripts to prevent duplicate execution when jobs run longer than their scheduled interval.
XI. Log Rotation Prep and CI/CD Placeholder Files
When setting up cron job based log rotation or CI/CD pipelines, placeholder files often need to exist before the actual process starts writing into them. Many tools check for the file's existence before they launch. touch handles this with zero overhead.
LinuxTeck.com
touch /var/log/myapp/access.log
touch /var/log/myapp/error.log
touch /var/log/myapp/debug.log
# In a CI pipeline create build artifact placeholders
touch /build/output/app.jar
touch /build/output/build.info
total 0
-rw-r--r-- 1 deploy deploy 0 May 08 11:20 access.log
-rw-r--r-- 1 deploy deploy 0 May 08 11:20 debug.log
-rw-r--r-- 1 deploy deploy 0 May 08 11:20 error.log
XII. Using touch in Docker and Container Environments
Inside Docker containers and container initialization scripts, touch is used to create PID files, ready-state markers, and lock files. Kubernetes health probes and orchestration systems can check for these files to confirm a container is fully initialized and ready to serve traffic.
LinuxTeck.com
RUN mkdir -p /app/run && touch /app/run/app.ready
# Health check probe script checks for this file
# HEALTHCHECK CMD test -f /app/run/app.ready || exit 1
---> Running in 3f9a1c2d8e44
Removing intermediate container 3f9a1c2d8e44
---> b7e21f3a9c10
Successfully built b7e21f3a9c10
Tip:
Using a ready-state marker file is a clean pattern for container health checks. It avoids complex HTTP probe setups when the container just needs to signal that initialization is complete before accepting requests.
Common touch Command Errors and Fixes
Most errors with touch come from filesystem permissions or path issues. Here are the ones you will run into most often and how to handle them without guessing.
I. Permission Denied
This happens when you try to create or update a file in a directory where your user does not have write permission.
LinuxTeck.com
Fix: Use sudo if you have the privilege, or adjust directory permissions using the chmod command in Linux to grant write access to your user account.
LinuxTeck.com
II. No Such File or Directory
This error appears when the parent directory in the path does not exist yet. touch will not create intermediate directories on its own.
LinuxTeck.com
Fix: Create the full directory path first with mkdir -p, then run touch.
LinuxTeck.com
III. Read-Only Filesystem
This typically happens on mounted ISO images, read-only NFS shares, or systems booted into recovery mode. The filesystem itself blocks all write operations.
Fix: Remount the filesystem with write access if appropriate, or do your work in a writable location like /tmp.
LinuxTeck.com
IV. SELinux Restrictions
On systems running SELinux in enforcing mode, which is the default on RHEL and Rocky Linux, touch may be blocked from creating files in certain directories even when standard Unix permissions look correct. The error message looks exactly like a standard permission denied error, which makes it tricky to diagnose without checking the audit log.
Fix: Check the SELinux audit log and restore the correct file context using restorecon.
LinuxTeck.com
restorecon -v /var/www/html/test.html
Using touch on Rocky Linux, RHEL, and Ubuntu
One of the best things about touch is that it works exactly the same way across all major Linux distributions. Whether you are on Ubuntu 24.04, RHEL 9, Rocky Linux 9, AlmaLinux, Debian, or any other mainstream distribution, the syntax, flags, and behavior are identical. There is nothing distro-specific to learn or worry about.
touch is part of the GNU Core Utilities package, which is installed by default on every major Linux distribution. You do not need to install anything extra. It is available the moment you open a terminal on any of these systems.
LinuxTeck.com
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
In enterprise environments on RHEL and Rocky Linux, touch is commonly used inside systemd service unit ExecStartPre hooks to create run directories and PID file placeholders before a service process starts. On Ubuntu-based servers, it shows up regularly in deployment scripts managed by tools like Fabric, Capistrano, and Ansible. Ansible playbooks often use touch via the file module or shell tasks to create marker files that track provisioning state across server fleets.
For full reference on all available options and edge case behavior, the GNU Coreutils official documentation is the authoritative source that covers the complete specification.
touch Command Cheat Sheet
| Task | Command |
|---|---|
| Create a single empty file | touch file.txt |
| Create multiple files at once | touch a.txt b.txt c.txt |
| Create 10 numbered files | touch file{1..10}.txt |
| Create zero-padded numbered files | touch log{01..20}.log |
| Update only the access time | touch -a file.txt |
| Update only the modification time | touch -m file.txt |
| Skip creation if file does not exist | touch -c file.txt |
| Set a custom timestamp | touch -t 202601011200 file.txt |
| Set timestamp with date string | touch -d "2026-03-15 08:30:00" file.txt |
| Copy timestamp from another file | touch -r source.txt target.txt |
| Check all timestamps of a file | stat file.txt |
| Check access time via ls | ls -l --time=atime file.txt |
Comparison: touch vs Similar Commands
Several Linux commands can produce an empty or new file, but they work differently. Knowing which one to use depends on what you actually need from the operation. You can also check the dedicated guide on the cat command in Linux and the echo command in Linux to understand how each one handles file creation differently.
| Command | Primary Purpose | Creates File? | Writes Content? | Updates Timestamp? |
|---|---|---|---|---|
| touch | Create empty file or update timestamps | Yes | No | Yes |
| echo | Write text content to a file | Yes (with redirect) | Yes | No (just writes) |
| cat | Create file with content from stdin | Yes (with redirect) | Yes | No (just writes) |
| truncate | Create or resize a file to a specific size | Yes | No (fills with zeros) | No |
| > file | Redirect operator creates or empties file | Yes | Empties it | No |
Warning:
Using echo "" > file.txt or > file.txt on an existing file will immediately wipe all its content. If you only need to update timestamps without destroying data, always use touch instead.
Why the touch Command Matters
The touch command is one of those utilities that keeps appearing in real infrastructure work. It is not flashy, but it solves specific problems cleanly. Creating placeholder files, managing lock mechanisms, setting up log directories before a service starts, signaling job completion in shell pipelines touch handles all of these with no overhead and no dependencies.
In automation and scripting, file existence and file timestamps are real control signals. Tools like make, rsync, and various backup utilities make important decisions based on these values. Being able to manipulate them directly with a single command is genuinely useful for a sysadmin or DevOps engineer working with real systems.
The fact that it works identically across Ubuntu, RHEL, Rocky Linux, Debian, and every other major distribution means you can write scripts that rely on touch without worrying about portability at all. It is always there, always behaves the same way, and never needs to be installed separately. For something that does so little on the surface, it earns its place in a lot of important workflows.
Key Takeaways
- touch creates empty files and updates file timestamps these are its two core functions and both are used regularly in real admin work
- Use -a to update only the access time and -m to update only the modification time without touching the other
- Use -c to prevent touch from creating a new file when the target does not already exist
- The -t flag sets a precise custom timestamp; use -d for a more readable date string format in scripts
- The -r flag copies timestamps from a reference file very useful in backup and restore workflows
- Brace expansion with touch is the fastest way to generate many files at once: touch file{01..20}.log
- Lock files created with touch are a standard bash scripting pattern for preventing duplicate cron job runs
- touch is part of GNU Core Utilities and available by default on all major Linux distributions without any installation needed
- Always verify timestamp changes using stat filename or ls -l --time=atime filename after running touch
- Never use echo redirect or the > operator to update timestamps on files that already have content you will lose that content permanently
People Also Ask
What does the touch command do in Linux?
The touch command serves two main purposes. First, it creates a new empty file if the specified file does not already exist. Second, if the file does exist, it updates the file's access and modification timestamps to the current time without changing any content inside the file. This makes it useful for placeholder file creation and for timestamp management in scripts and automation workflows.
How to create multiple files using the touch command?
You can pass multiple filenames to touch in one command: touch file1.txt file2.txt file3.txt. For creating many numbered files at once, use brace expansion: touch file{1..10}.txt creates file1.txt through file10.txt in a single command. For zero-padded filenames, use: touch log{01..20}.log which creates log01.log through log20.log.
Does the touch command overwrite existing files?
No, touch does not overwrite or modify the content of existing files at all. When you run touch on a file that already has content, it only updates the timestamps. The data inside the file stays completely unchanged. This is a key reason to prefer touch over echo redirect or the > operator when you only need to change a timestamp and not the file data.
Can the touch command create directories?
No, touch cannot create directories. It only creates regular files. If you need to create a directory, use the mkdir command. If you need to create a directory along with a file inside it, use mkdir -p /path/to/dir first, then run touch on the file path inside that directory.
Why is the timestamp not changing when I use touch?
A few things can cause this. The most common reason is filesystem mount options some filesystems are mounted with relatime or noatime, which restricts how frequently atime gets updated. SELinux in enforcing mode can also deny the operation silently without obvious error output. Check with stat filename for the most accurate timestamp view, and run ausearch -m avc -ts recent to check for any SELinux denials in the audit log.
What is the difference between touch and echo in Linux?
touch creates an empty file or updates timestamps without writing any content into the file. echo with a redirect (echo "text" > file) writes text content into a file. If the file already has content, echo with the > redirect will overwrite and erase everything in it, while touch will leave the content completely untouched. Use touch when you need an empty placeholder or a timestamp update. Use echo when you actually need to write or append something to the file.
From your first terminal command to advanced sysadmin skills — every guide here is written in plain English with real examples you can run right now.