opscape

Files and directories

touch

Update a file's timestamps, creating it empty if it does not exist.

touch [OPTION...] FILE...

10 of 10 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: touch(1)

touch sets a file's access and modification times to now. If the file does not exist, it creates an empty one, which is what most people use it for.

With -d or -t you can set any time you like, and -r copies the times from another file. That matters for anything that compares timestamps: make rebuilds targets older than their sources, find -newer and -mtime select by age, rsync and backup tools skip files whose time and size have not changed.

touch never changes a file's contents. To empty a file, use : > file or truncate.

Examples

Create an empty file

touch new.txt
ls -l new.txt
output
-rw-r--r-- 1 dev dev 0 Oct 31 20:21 new.txt

The file is created with size 0 and the current time.

Create several files at once

touch day{1..3}.md
ls day*
output
day1.md
day2.md
day3.md

Brace expansion produces day1.md day2.md day3.md; touch creates each one.

Bump the modification time of an existing file

ls -l todo.txt
touch todo.txt
ls -l todo.txt
output
-rw-rw-r-- 1 dev dev 62 Oct 24 21:28 todo.txt
-rw-rw-r-- 1 dev dev 62 Oct 31 20:21 todo.txt

The content and size are unchanged; only the time moves to now. This is how you force make to rebuild something, or make a file count as "recent".

Set a specific date and time

touch -d "2025-12-31 23:59" report.txt
ls -l --time-style=long-iso report.txt
output
-rw-r--r-- 1 dev dev 0 2025-12-31 23:59 report.txt

-d accepts the same free-form dates as date -d, such as "yesterday" or "2 hours ago". Here it set a fixed timestamp on a new file.

Backdate a file

touch -d "3 days ago" old.txt
find . -maxdepth 1 -name '*.txt' -mtime +2
output
./todo.txt
./notes.txt
./file with spaces.txt
./old.txt

find -mtime +2 matches files modified more than two full days ago. The new old.txt qualifies, and so do the fixture's own text files, which are about a week old.

Use -t with the classic stamp format

touch -t 202601150930.00 meeting.txt
stat -c '%y %n' meeting.txt
output
2026-01-15 09:30:00.000000000 +0000 meeting.txt

-t takes [[CC]YY]MMDDhhmm[.ss], the form that also works on BSD and macOS. stat shows the full timestamp.

Copy timestamps from another file

cp notes.txt notes-copy.txt
touch -r notes.txt notes-copy.txt
ls -l notes.txt notes-copy.txt
output
-rw-r--r-- 1 dev dev 190 Oct 25 05:30 notes-copy.txt
-rw-rw-r-- 1 dev dev 190 Oct 25 05:30 notes.txt

The copy got a new time from cp; -r sets it back to exactly what notes.txt has.

Update only files that exist

touch -c todo.txt missing.txt
ls todo.txt missing.txt
outputexit 2
ls: cannot access 'missing.txt': No such file or directory
todo.txt

-c (--no-create) touches todo.txt but does not create missing.txt, so ls can still not find it.

Change only the modification time

touch -m -d "2026-01-01" todo.txt
stat --printf 'modify: %y\naccess: %x\n' todo.txt
output
modify: 2026-01-01 00:00:00.000000000 +0000
access: 2026-10-24 21:28:49.000000000 +0000

-m changes only the modification time; -a only the access time. The access time here is left as it was.

Touching a file in a directory you cannot write to

touch /etc/new.conf
explain
outputexit 1
touch: cannot touch '/etc/new.conf': Permission denied

Creating a file needs write permission on the directory. Updating an existing file's time to "now" only needs write permission on the file; setting an arbitrary time needs ownership.

Common pitfalls

  • A typo creates a new empty file silently: touch confg.json does not warn. Use touch -c when you only mean to update existing files.

  • touch does not create directories; touch logs/2026/app.log fails if logs/2026 is missing. Run mkdir -p first.

  • There is no option to set the creation (birth) time; most filesystems record it but only the kernel sets it.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • -d with free-form dates like "3 days ago" is GNU. BSD/macOS touch -d takes only an ISO-style YYYY-MM-DDThh:mm:ss; -t works the same on both.

  • macOS adds -A to adjust times by an offset; on GNU use -d with a relative date instead.

Options worth knowing

A selection, described in our own words. For the full list, see touch(1) or touch --help.

OptionWhat it does
-aChange only the access time.
-mChange only the modification time.
-c, --no-createDo not create files that do not exist.
-d, --date STRINGUse the time described by STRING instead of now.
-t STAMPUse [[CC]YY]MMDDhhmm[.ss] instead of now.
-r, --reference FILEUse FILE's times instead of now.
-h, --no-dereferenceChange a symlink's own times, not its target's.
--time WORDChange only atime (access, use) or mtime (modify).

Related