The touch command in Linux is a versatile utility used primarily to create empty files or update file timestamps (access time and modification time). If the specified file does not exist, touch creates it as an empty file with default permissions based on the user's umask. If the file already exists, touch updates its access and modification times to the current system time.
Key Functions:
Create an empty file:
touch filenamecreates a new empty file namedfilename.Update timestamps:
If the file exists,touchupdates both access time (atime) and modification time (mtime) to the current time.Update only access time:
Usetouch -a filenameto update only the access time.Update only modification time:
Usetouch -m filenameto update only the modification time.Set custom timestamps:
Usetouch -d 'DATE' filenameto set a specific date and time (e.g.,touch -d '2025-12-25 14:30' file.txt).Use reference file timestamps:
Usetouch -r ref_file.txt target_file.txtto copy timestamps fromref_file.txttotarget_file.txt.Prevent file creation:
Usetouch -c filenameto update timestamps only if the file exists — otherwise, do nothing.Modify symbolic link timestamps:
Usetouch -h symlinkto update the symlink’s timestamp instead of the target file.
Note: The
ctime(change time) is automatically updated by the kernel whenever file metadata or content changes, including whentouchmodifiesatimeormtime. It cannot be set directly withtouch.
Example Usage:
touch newfile.txt # Creates empty file
touch -a log.txt # Updates only access time
touch -m -d "2026-03-03" data.txt # Sets mtime to March 3, 2026
touch -r template.txt config.txt # Copies timestamps from template to configUse stat filename to verify timestamp changes after running touch.
People always seem to touch a new file before writing to it. But why? Considering that file gets created automatically when you try to write to it. For instance, I see people doing
touch myfile.txt nano myfile.txt
When just doing nano myfile.txt would have already worked. Also,
touch log echo ERROR > log
Could just very well be echo ERROR > log
Why?
Actually, I learned that very ancient distros of linux, linux 2.xish used to give you error if you tried to write to a file that does not exist. But we are not in the 1990s any more
How to use the touch command in Linux?
How do I use the touch command in Linux?
How to use touch command in linux bash script
Try redirecting a blank echo to create the file.
echo -n > myemptyfile.conf
Additionally, ensure the user and/or script has permission to write to the output directory. Also confirm that the file isn't already being created, but going somewhere you didn't realize (such as the current working directory if you're not providing an explicit path for the output).
More on reddit.com9 Useful Examples of Touch Command in Linux
What is the practical use of assessing or updating when you last opened a file?
More on reddit.comVideos
You can use find command to find all your files and execute touch on every found file using -exec
find . -type f -exec touch {} +
If you want to filter your result only for text files, you can use
find . -type f -name "*.txt" -exec touch {} +
touch **/**
This updates the timestamps of all files and directories recursively within the current directory when using Zsh or bash -O globstar.
Using the append redirector ">>" resolves the issue where an existing file is deleted:
echo $null >> filename
To create a blank file:
New-Item example.txt
Note that in old versions of PowerShell, you may need to specify -ItemType file .
To update the timestamp of a file:
(gci example.txt).LastWriteTime = Get-Date