1. Introduction
The touch
command in Linux is a versatile utility with two primary functions: updating the access and modification timestamps of files or directories to the current time or a specified time, and creating empty files if they do not exist. Its name reflects the metaphorical act of “touching” a file to update its metadata without altering its content. Widely used in scripting, software development, and system administration, touch
is essential for tasks like triggering build processes, simulating file changes, or managing file metadata.
This guide provides a detailed overview, structured like the mv
command guide, with numbered sections for clarity. It includes practical examples, explanatory comments, and answers to common questions sourced from forums such as Stack Overflow and Unix & Linux Stack Exchange . The content is validated against the official Linux touch
man page from GNU Coreutils to ensure accuracy and completeness.
2. Basic Syntax
The touch
command follows this syntax:
$ touch [OPTION]... FILE...
[OPTION]...
: Optional flags that modify behavior, such as specifying which timestamp to update or preventing file creation.FILE...
: One or more filenames or paths to files/directories whose timestamps will be updated or created if they do not exist.
Note: touch
is typically an external command (/bin/touch
), but some shells may include a built-in version. This guide focuses on the GNU Coreutils implementation, standard in most Linux distributions.
3. Core Use Cases with Examples
To demonstrate touch
’s functionality, we set up a test environment with a basic file and directory structure:
# Navigate to home directory
$ cd ~
# Create a test directory and navigate into it
$ mkdir touch_test
$ cd touch_test
# Create sample files and directories
$ echo "Initial content." > existing_file.txt
$ touch note.txt report.pdf data.csv
$ mkdir logs scripts
$ echo "Log entry" > logs/app.log
$ echo '#!/bin/bash' > scripts/myscript.sh
$ chmod +x scripts/myscript.sh
$ ln -s note.txt note_link
# Verify setup
$ ls -p
data.csv existing_file.txt logs/ note.txt note_link@ report.pdf scripts/
Comment: In the examples below, ls
output shows directory names with a trailing /
(e.g., logs/
) for clarity, even though ls
without -p
or -F
does not append /
. To view directories with /
in real output, use ls -p
or ls -F
:
# Correct usage to show directory bifurcation
$ ls -p
data.csv existing_file.txt logs/ note.txt note_link@ report.pdf scripts/
3.1. Creating a New Empty File
Creates an empty file if it does not exist.
Input:
# Create a new empty file
$ touch new_file.log
# Verify creation
$ ls -l new_file.log
Output:
-rw-r--r-- 1 user user 0 May 4 20:44 new_file.log
Comment: The file is created with zero size, ideal for placeholders or log files.
Directory Impact: Adds new_file.log
to the touch_test
directory.
3.2. Creating Multiple New Empty Files
Creates multiple empty files in a single command.
Input:
# Create multiple files
$ touch config.yaml temp.tmp backup.dat
# Verify
$ ls -l config.yaml temp.tmp backup.dat
Output:
-rw-r--r-- 1 user user 0 May 4 20:44 backup.dat
-rw-r--r-- 1 user user 0 May 4 20:44 config.yaml
-rw-r--r-- 1 user user 0 May 4 20:44 temp.tmp
Comment: Multiple filenames are processed sequentially, creating each as needed.
Directory Impact: Adds config.yaml
, temp.tmp
, and backup.dat
to the directory.
3.3. Updating Timestamps of an Existing File
Updates both access and modification times of an existing file to the current time.
Input:
# Check initial timestamp
$ stat -c "%x %y" existing_file.txt
# Wait to ensure time difference
$ sleep 2
# Update timestamps
$ touch existing_file.txt
# Verify updated timestamp
$ stat -c "%x %y" existing_file.txt
Output:
2025-05-04 20:44:00.000000000 +0530 2025-05-04 20:44:00.000000000 +0530
2025-05-04 20:44:02.000000000 +0530 2025-05-04 20:44:02.000000000 +0530
Comment: The file’s content remains unchanged; only timestamps are updated.
Directory Impact: No new files; existing_file.txt
timestamps updated.
3.4. Updating Directory Timestamps
Updates timestamps of a directory.
Input:
# Check initial directory timestamp
$ stat -c "%x %y" logs
# Update directory timestamp
$ touch logs
# Verify updated timestamp
$ stat -c "%x %y" logs
Output:
2025-05-04 20:44:00.000000000 +0530 2025-05-04 20:44:00.000000000 +0530
2025-05-04 20:44:02.000000000 +0530 2025-05-04 20:44:02.000000000 +0530
Comment: Directories are treated like files for timestamp updates.
Directory Impact: No new files; logs
directory timestamps updated.
3.5. Setting Specific Timestamps
Sets timestamps to a specific date using -d
.
Input:
# Set timestamp to a specific date
$ touch -d "2024-12-25 08:00:00" note.txt
# Verify
$ ls -l --full-time note.txt
Output:
-rw-r--r-- 1 user user 0 2024-12-25 08:00:00.000000000 +0530 note.txt
Comment: The -d
option supports flexible date strings, useful for testing or synchronization.
Directory Impact: No new files; note.txt
timestamps updated.
3.6. Using a Reference File
Sets timestamps to match another file’s using -r
.
Input:
# Create reference file with specific timestamp
$ touch -d "2025-05-01 00:00:00" ref_file.txt
# Check reference timestamp
$ stat -c "%x %y" ref_file.txt
# Update target file using reference
$ touch -r ref_file.txt report.pdf
# Verify target timestamp
$ stat -c "%x %y" report.pdf
Output:
2025-05-01 00:00:00.000000000 +0530 2025-05-01 00:00:00.000000000 +0530
2025-05-01 00:00:00.000000000 +0530 2025-05-01 00:00:00.000000000 +0530
Comment: Matches report.pdf
timestamps to ref_file.txt
.
Directory Impact: Adds ref_file.txt
; updates report.pdf
timestamps.
3.7. Preventing File Creation
Uses -c
to avoid creating non-existent files.
Input:
# Attempt to touch non-existent file
$ touch -c non_existent.txt
# Verify no creation
$ ls non_existent.txt
Output:
ls: cannot access 'non_existent.txt': No such file or directory
Comment: The -c
option ensures no new file is created.
Directory Impact: No change.
3.8. Handling Filenames with Spaces
Creates or updates files with spaces in names.
Input:
# Create file with spaces
$ touch "file with spaces.txt"
# Verify
$ ls -l "file with spaces.txt"
Output:
-rw-r--r-- 1 user user 0 May 4 20:44 file with spaces.txt
Comment: Quoting ensures proper handling of spaces.
Directory Impact: Adds file with spaces.txt
.
4. Key Options Explained (with Examples)
The touch
command offers options for precise timestamp control, validated against the GNU Coreutils touch
documentation.
4.1. -a
(Change Only Access Time)
Updates only the access time (atime).
Input:
# Check initial times
$ stat existing_file.txt | grep 'Access\|Modify'
# Update access time
$ sleep 2
$ touch -a existing_file.txt
# Verify
$ stat existing_file.txt | grep 'Access\|Modify'
Output:
Access: 2025-05-04 20:44:00.123456789 +0530
Modify: 2025-05-04 20:44:00.123456789 +0530
Access: 2025-05-04 20:44:02.987654321 +0530
Modify: 2025-05-04 20:44:00.123456789 +0530
Comment: Only atime is updated, leaving mtime unchanged.
4.2. -m
(Change Only Modification Time)
Updates only the modification time (mtime).
Input:
# Check initial times
$ stat existing_file.txt | grep 'Access\|Modify'
# Update modification time
$ sleep 2
$ touch -m existing_file.txt
# Verify
$ stat existing_file.txt | grep 'Access\|Modify'
Output:
Access: 2025-05-04 20:44:02.987654321 +0530
Modify: 2025-05-04 20:44:00.123456789 +0530
Access: 2025-05-04 20:44:02.987654321 +0530
Modify: 2025-05-04 20:44:04.112233445 +0530
Comment: Only mtime is updated, useful for build triggers.
4.3. -c
, --no-create
(Do Not Create Files)
Prevents file creation if the file does not exist.
Input:
# Attempt to touch non-existent file
$ touch -c non_existent.txt
# Verify
$ ls non_existent.txt
Output:
ls: cannot access 'non_existent.txt': No such file or directory
Comment: Ensures only existing files are modified.
4.4. -r FILE
, --reference=FILE
(Use Reference File’s Times)
Sets timestamps to match another file’s.
Input:
# Set reference file timestamp
$ touch -d "2025-05-01 00:00:00" ref_file.txt
# Update target using reference
$ touch -r ref_file.txt data.csv
# Verify
$ stat -c "%x %y" data.csv
Output:
2025-05-01 00:00:00.000000000 +0530 2025-05-01 00:00:00.000000000 +0530
Comment: Synchronizes timestamps across files.
4.5. -d STRING
, --date=STRING
(Use Specific Date)
Sets timestamps using a date string.
Input:
# Set relative date
$ touch -d "yesterday" report.pdf
# Verify
$ ls -l report.pdf
Output:
-rw-r--r-- 1 user user 0 May 3 00:00 report.pdf
Comment: Supports flexible date formats, e.g., “2 days ago”.
4.6. -t STAMP
(Use Specific Timestamp Format)
Sets timestamps using a numeric format: [[CC]YY]MMDDhhmm[.ss]
.
Input:
# Set timestamp to May 4, 2025, 09:30:15
$ touch -t 202505040930.15 note.txt
# Verify
$ ls -l --full-time note.txt
Output:
-rw-r--r-- 1 user user 0 2025-05-04 09:30:15.000000000 +0530 note.txt
Comment: Precise control for specific timestamps.
4.7. --time=WORD
(Specify Timestamp to Change)
Selects which timestamp to update (access
or modify
).
Input:
# Set access time to yesterday
$ touch --time=access -d "yesterday" data.csv
# Verify
$ stat data.csv | grep 'Access\|Modify'
Output:
Access: 2025-05-03 00:00:00.000000000 +0530
Modify: 2025-05-01 00:00:00.000000000 +0530
Comment: Equivalent to -a
or -m
when used with -d
or -t
.
4.8. --help
(Display Help)
Shows usage information.
Input:
$ touch --help
Output (abridged):
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.
...
Comment: Quick reference for options.
4.9. --version
(Display Version)
Shows version information.
Input:
$ touch --version
Output (abridged):
touch (GNU coreutils) 8.32
...
Comment: Useful for troubleshooting.
5. Combining Options
Combining options enables precise timestamp control:
5.1. Update Modification Time Only, No Creation
Input:
# Update mtime only if file exists
$ touch -c -m non_existent.txt
$ ls non_existent.txt
# Update mtime of existing file
$ touch -c -m existing_file.txt
$ stat existing_file.txt | grep 'Modify'
Output:
ls: cannot access 'non_existent.txt': No such file or directory
Modify: 2025-05-04 20:44:06.000000000 +0530
Comment: Combines -c
and -m
for safe updates.
5.2. Set Access Time to Reference File
Input:
# Set atime using reference
$ touch -a -r ref_file.txt report.pdf
$ stat report.pdf | grep 'Access'
Output:
Access: 2025-05-01 00:00:00.000000000 +0530
Comment: Updates only atime to match reference.
5.3. Set Specific Date for Modification Time
Input:
# Set mtime to specific date
$ touch -m -d "2024-12-01" data.csv
$ ls -l data.csv
Output:
-rw-r--r-- 1 user user 0 Dec 1 00:00 data.csv
Comment: Combines -m
and -d
for targeted updates.
6. Handling Special Cases
6.1. Filenames Starting with Hyphen
Use --
to treat hyphens as part of the filename.
Input:
$ touch -- -file.txt
$ ls -l -- -file.txt
Output:
-rw-r--r-- 1 user user 0 May 4 20:44 -file.txt
Comment: Prevents misinterpretation of hyphens as options.
6.2. Filenames with Spaces
Quote or escape spaces in filenames.
Input:
$ touch "file with spaces.txt"
$ ls -l "file with spaces.txt"
Output:
-rw-r--r-- 1 user user 0 May 4 20:44 file with spaces.txt
Comment: Ensures proper handling of spaces.
6.3. Permissions
Requires write permission in the directory to create files or write permission on the file to update timestamps.
Input:
# Attempt to touch in restricted directory
$ touch /root/test.txt
Output:
touch: cannot touch '/root/test.txt': Permission denied
Comment: Check permissions before using touch
.
6.4. Directories
Updates directory timestamps like files.
Input:
$ touch scripts/
$ stat scripts | grep 'Access\|Modify'
Output:
Access: 2025-05-04 20:44:06.000000000 +0530
Modify: 2025-05-04 20:44:06.000000000 +0530
Comment: Useful for directory metadata management.
7. Frequently Asked Questions (FAQ)
7.1. What is the main purpose of the touch
command?
The primary purpose of touch
is to update the access and modification timestamps of a file to the current time or a specified time. It also creates a new empty file if the specified file does not exist, making it a convenient tool for file creation.
Example:
# Create or update file.txt
$ touch file.txt
$ ls -l file.txt
Output:
-rw-r--r-- 1 user user 0 May 4 20:44 file.txt
7.2. How can I create a new file with touch
?
Use touch filename
to create a new empty file. If the file already exists, its timestamps are updated without altering its content.
Example:
# Create a new empty file
$ touch new_file.txt
$ ls -l new_file.txt
Output:
-rw-r--r-- 1 user user 0 May 4 20:44 new_file.txt
7.3. Can I update only the access time or only the modification time?
Yes, the -a
option updates only the access time (atime), and the -m
option updates only the modification time (mtime).
Example:
# Update access time only
$ touch -a file.txt
$ stat file.txt | grep 'Access'
# Update modification time only
$ touch -m file.txt
$ stat file.txt | grep 'Modify'
Output:
Access: 2025-05-04 20:44:02.987654321 +0530
Modify: 2025-05-04 20:44:04.112233445 +0530
7.4. How do I set a specific timestamp for a file?
Use the -d
option with a date string or -t
with a numeric timestamp format (e.g., [[CC]YY]MMDDhhmm[.ss]
).
Example:
# Set timestamp to Dec 25, 2024, 08:00
$ touch -d "2024-12-25 08:00:00" file.txt
$ ls -l --full-time file.txt
Output:
-rw-r--r-- 1 user user 0 2024-12-25 08:00:00.000000000 +0530 file.txt
7.5. What happens if I use touch
on a directory?
touch
updates the timestamps of the directory itself, not its contents, treating it like a file for timestamp purposes.
Example:
# Update directory timestamps
$ touch mydir/
$ stat mydir | grep 'Access\|Modify'
Output:
Access: 2025-05-04 20:44:06.000000000 +0530
Modify: 2025-05-04 20:44:06.000000000 +0530
7.6. Can I use touch
to create multiple files at once?
Yes, specify multiple filenames in a single command to create or update them.
Example:
# Create multiple files
$ touch file1.txt file2.txt file3.txt
$ ls -l file1.txt file2.txt file3.txt
Output:
-rw-r--r-- 1 user user 0 May 4 20:44 file1.txt
-rw-r--r-- 1 user user 0 May 4 20:44 file2.txt
-rw-r--r-- 1 user user 0 May 4 20:44 file3.txt
7.7. Why would I want to change a file’s timestamp?
Changing timestamps is useful for triggering build systems (e.g., make
), simulating file changes, testing time-based logic, or sorting files by modification time with ls -t
.
Example:
# Update timestamp to trigger build
$ touch source.c
$ make
Output (example):
gcc -o program source.c
7.8. Does touch
change the creation time (birth time) of a file?
No, touch
does not modify the birth time (creation time), as it is not universally tracked by filesystems. It only updates access and modification times.
Example:
# Check birth time
$ stat file.txt | grep 'Birth'
Output:
Birth: -
7.9. How does touch
handle symbolic links?
By default, touch
updates the timestamps of the symbolic link itself. Use -h
or --no-dereference
to update the target file’s timestamps.
Example:
# Create a symbolic link
$ ln -s target.txt link.txt
# Update link's timestamps
$ touch link.txt
# Update target's timestamps
$ touch -h link.txt
$ ls -l link.txt
Output:
lrwxrwxrwx 1 user user 9 May 4 20:44 link.txt -> target.txt
7.10. Are there any security implications of using touch
?
Creating files requires write permission in the target directory, and updating timestamps requires write permission on the file or ownership. Improper use in restricted directories can lead to permission errors, but touch
itself poses minimal security risks.
Example:
# Attempt to touch in restricted directory
$ touch /root/test.txt
Output:
touch: cannot touch '/root/test.txt': Permission denied
7.11. Can I use touch
to create a file with specific permissions?
No, touch
creates files with default permissions (typically 644
, modified by umask). Use chmod
to set specific permissions afterward.
Example:
# Create file and set permissions
$ touch new_file.txt
$ chmod 600 new_file.txt
$ ls -l new_file.txt
Output:
-rw------- 1 user user 0 May 4 20:44 new_file.txt
7.12. How does touch
behave with wildcards?
Wildcards are expanded by the shell, allowing touch
to create or update multiple files matching a pattern.
Example:
# Create files with pattern
$ touch file{1..3}.txt
$ ls -l file*.txt
Output:
-rw-r--r-- 1 user user 0 May 4 20:44 file1.txt
-rw-r--r-- 1 user user 0 May 4 20:44 file2.txt
-rw-r--r-- 1 user user 0 May 4 20:44 file3.txt
7.13. Can touch
update timestamps recursively for all files in a directory?
No, touch
does not support recursive updates. Use find
with touch
for recursive operations.
Example:
# Update timestamps of all files in directory
$ find . -type f -exec touch {} \;
$ ls -l
7.14. What is the difference between touch -c
and touch --no-create
?
They are identical; both prevent touch
from creating new files, only updating existing ones.
Example:
# No creation with -c
$ touch -c non_existent.txt
$ ls non_existent.txt
Output:
ls: cannot access 'non_existent.txt': No such file or directory
7.15. How can I test if a file exists using touch
?
Use touch -c
and check the exit status ($?
): 0 if the file exists, 1 if it does not.
Example:
# Test file existence
$ touch -c existing_file.txt
$ echo $?
0
$ touch -c non_existent.txt
$ echo $?
1
7.16. Why update a file’s timestamp?
To trigger build systems, signal script events, test time-based logic, or sort files.
Input:
$ touch -m source.c
$ make
Output:
gcc -o program source.c
7.17. How is touch
different from mkdir
?
touch
manages file timestamps or creates empty files; mkdir
creates directories.
Input:
$ touch dir.txt
$ mkdir dir
$ ls -p
dir/ dir.txt
Output:
dir/ dir.txt
7.18. How to create a file with content?
Use echo
or editors, as touch
creates empty files.
Input:
$ echo "Content" > file.txt
$ cat file.txt
Output:
Content
7.19. Does touch
change ctime?
Yes, updating atime or mtime also updates ctime due to inode metadata changes.
Input:
$ stat existing_file.txt | grep 'Change'
$ touch existing_file.txt
$ stat existing_file.txt | grep 'Change'
Output:
Change: 2025-05-04 20:44:00.000000000 +0530
Change: 2025-05-04 20:44:06.000000000 +0530
7.20. Can I set timestamps older than creation time?
Yes, atime and mtime can be set to any time, even before birth time.
Input:
$ touch -d "2020-01-01" file.txt
$ ls -l file.txt
Output:
-rw-r--r-- 1 user user 0 Jan 1 00:00 file.txt
8. Conclusion
The touch
command is a deceptively simple yet powerful tool in Linux, serving dual purposes: updating file and directory timestamps and creating empty files. Its flexibility shines in scenarios like triggering build systems, testing time-sensitive applications, or preparing placeholder files for scripts. Options such as -a
, -m
, -d
, and -r
provide precise control over timestamp manipulation, while -c
and -h
handle edge cases like non-existent files and symbolic links. By integrating touch
with commands like find
or chmod
, users can extend its utility for complex tasks. Whether you’re a developer, system administrator, or casual user, mastering touch
enhances your ability to manage Linux filesystems efficiently and effectively.
9. touch Command: Reference Table of Key Options
Option(s) | Description | Example Command | Use Case |
---|---|---|---|
(none) | Update atime & mtime to now, or create empty file | touch file.txt | Create file if missing, update times if exists |
-a | Change only access time (atime) | touch -a file.txt | Update last read time without changing modification time |
-m | Change only modification time (mtime) | touch -m file.txt | Update last modified time, e.g., to trigger build tools |
-c , --no-create | Do not create file if it doesn’t exist | touch -c file.txt | Only update timestamps of existing files |
-r FILE , --reference=FILE | Use reference FILE’s times instead of current time | touch -r ref.log target.log | Synchronize timestamps between two files |
-d STRING , --date=STRING | Use specified date STRING instead of current time | touch -d "yesterday" f.txt | Set timestamp to a specific or relative date/time |
-t STAMP | Use specific timestamp [[CC]YY]MMDDhhmm[.ss] | touch -t 202501151000 f.txt | Set timestamp using a precise numeric format |
--time=WORD | Specify which time (access or modify) to set | touch -d "now" --time=access file | Explicitly set only atime or mtime when using -d or -t |
-h , --no-dereference | Affect symbolic link instead of referenced file | touch -h link.txt | Update the link’s timestamps, not the target’s |
-- | Signify end of options | touch -- -filename.txt | Handle filenames that start with a hyphen |
--help | Display help information | touch --help | View command usage and options |
--version | Display version information | touch --version | Check command version for troubleshooting |