What it does
touch FILE creates FILE when it does not exist. When it exists, touch sets its times to now, without changing the content.
This is used to make test files, to create flag files that other programs look for (like /.autorelabel for SELinux), and to set exact times for tests of backup or cleanup scripts.
How to write it
touch [OPTION]... FILE...Options 11
Most used
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Create an empty file
touch notes.txtIf it already exists, only its time changes.
2Create many files at once
touch file{1..5}.txtMakes file1.txt to file5.txt. Good for practice.
3Ask SELinux to relabel the whole system at the next boot
touch /.autorelabelNeeded after a root password reset with rd.break, or after SELinux was disabled.
4Set a file to an old date to test a cleanup script
touch -d "40 days ago" /tmp/test-old.log find /tmp -name "*.log" -mtime +30The file looks 40 days old, so your
find -mtime +30test can find it.5Give a file the same time as another file
touch -r original.conf copy.confKeeps times equal, for example after you fix a copy.
6Set an exact time with -t
touch -t 202601011200 newyear.txt2026-01-01 12:00.
7Change only the access time
touch -a report.pdfThe modification time stays the same.
8Do not create a missing file
touch -c maybe.txtNo error and no new file if it does not exist.
9Create a flag file for a maintenance window
touch /etc/nologinWhile this file exists, normal users cannot log in (root still can). Remove it after the work.
10Make an empty log file with the right owner
touch /var/log/myapp.log && chown myapp: /var/log/myapp.logThe service can write to it at its first start.
11Check if a folder is writable in a script
A simple test before a backup job writes to a share.
Nothing matches this filter.
Watch out
touchon an existing file does not empty it. To empty a file use> fileortruncate -s 0 file.- Changing times can hide what really happened on a system; in forensics, look at ctime (it cannot be set by touch).
Tips to remember
- New empty file or "update the time" — both are touch.
/.autorelabel= SELinux relabel at the next boot.