Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

touch

Create an empty file, or change the access and modification times of a file.

Level
RHCSA exam
Group
Files and folders
Needs root
No
Package
coreutils

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

-a
Change only the access time.
-m
Change only the modification time.
-c, --no-create
Do not create files that do not exist.
-d, --date=text
Use this date and time instead of now, like "2026-01-31 14:00" or "yesterday".
-t stamp
Use this time in the form [[CC]YY]MMDDhhmm[.ss].
-r, --reference=file
Use the times of this other file.
-h, --no-dereference
Change the link itself, not the file it points to.
--time=word
Which time to change: access (atime) or modify (mtime).
-f
Ignored (kept for compatibility).
--help
Show help.
--version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Create an empty file

    touch notes.txt

    If it already exists, only its time changes.

  2. 2Create many files at once

    touch file{1..5}.txt

    Makes file1.txt to file5.txt. Good for practice.

  3. 3Ask SELinux to relabel the whole system at the next boot

    touch /.autorelabel

    Needed after a root password reset with rd.break, or after SELinux was disabled.

  4. 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 +30

    The file looks 40 days old, so your find -mtime +30 test can find it.

  5. 5Give a file the same time as another file

    touch -r original.conf copy.conf

    Keeps times equal, for example after you fix a copy.

  6. 6Set an exact time with -t

    touch -t 202601011200 newyear.txt

    2026-01-01 12:00.

  7. 7Change only the access time

    touch -a report.pdf

    The modification time stays the same.

  8. 8Do not create a missing file

    touch -c maybe.txt

    No error and no new file if it does not exist.

  9. 9Create a flag file for a maintenance window

    touch /etc/nologin

    While this file exists, normal users cannot log in (root still can). Remove it after the work.

  10. 10Make an empty log file with the right owner

    touch /var/log/myapp.log && chown myapp: /var/log/myapp.log

    The service can write to it at its first start.

  11. 11Check if a folder is writable in a script

    touch /mnt/nfs/.write-test && rm /mnt/nfs/.write-test && echo "writable"

    A simple test before a backup job writes to a share.

Watch out

  • touch on an existing file does not empty it. To empty a file use > file or truncate -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.