Linux Command Reference ยท Files and Directories
Linux touch command
Reference for the Linux touch command: create empty files, set access and modification times with -d, -t and -r, avoid creating files with -c.
touch does two jobs. If a file does not exist, it creates an empty one. If it does exist, it updates the file's access time and modification time, normally to the current moment, without touching the content.
With options you can set the times to any value, copy them from another file, or change only one of them. Build tools such as make rely on modification times, so touch is the standard way to mark a file as "changed" or to force a rebuild.
touch [OPTION]... FILE...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
touch [-acfhm] [-d DATE | -t STAMP | -r FILE] FILE...
| Option | Description |
|---|---|
-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, --date=DATE |
Use a date string such as 2024-03-15 10:30, yesterday or @1700000000 (Unix seconds). |
-t STAMP |
Use [[CC]YY]MMDDhhmm[.ss]. |
-r FILE, --reference=FILE |
Copy the times from FILE. |
-h, --no-dereference |
Change the time of a symlink itself, not its target. |
Returns: 0 on success; non-zero if any file could not be created or updated (for example "Permission denied" or "No such file or directory" when the parent directory is missing).
Usage notes
- Without
-aor-mboth times are updated. With either flag, the other time keeps its value. - The inode change time (
ctime) is always set to now and cannot be set bytouch; it records that something changed. - Date strings are parsed by GNU
daterules, so relative forms work:touch -d "2 days ago" file. Time zone matters: addUTCor setTZfor repeatable results. - Creating many files:
touch file{1..100}.txt. The shell expands the braces beforetouchsees them. - Existing content is never truncated.
touch fileis safe on a file with data;: > fileor> fileempties it. - Permission rules: to set an arbitrary time you must own the file (or be root). If you only have write permission, you can update to "now", but not to a chosen time.
- Timestamps are precise to nanoseconds on modern filesystems, but some formats (FAT) store only two-second resolution.
touchon a directory updates the directory's times; it does not create anything inside.- Makefile tip:
touch -c targetmarks an up-to-date target without creating it by accident.
Related
ls,mkdir,cp,stat- Lessons: Creating files and directories, Finding files