TExcelTAD
Home › Linux & Bash › touch: Create Files

touch: Create Files

touch has two jobs: it creates empty files, and it updates the timestamps of existing ones. The first is what beginners use it for; the second is what build tools like make rely on.

Creating empty files

bash · try it yourself
touch notes.txt
touch index.html style.css app.js
ls
stat -c '%s bytes: %n' notes.txt
app.js
index.html
notes.txt
style.css
0 bytes: notes.txt

If a file already exists, touch leaves its content alone; it only changes its times. That makes it safe to run on existing files, unlike > file, which empties them:

bash · try it yourself
echo "important data" > keep.txt
touch keep.txt
cat keep.txt
> keep.txt
stat -c '%s bytes after > keep.txt' keep.txt
important data
0 bytes after > keep.txt

Brace expansion: many files at once

bash · try it yourself
touch chapter-{1..5}.md
touch test_{api,db,ui}.py
ls
chapter-1.md
chapter-2.md
chapter-3.md
chapter-4.md
chapter-5.md
test_api.py
test_db.py
test_ui.py

Timestamps

Every file has three times:

Time Changed when stat code
Modification (mtime) The content changes %y
Access (atime) The file is read (often updated lazily) %x
Change (ctime) Content or metadata (permissions, owner) change %z

ls -l shows the mtime and ls -t sorts by it. touch sets atime and mtime to the current time, or to a time you choose with -d (a date string) or -t ([[CC]YY]MMDDhhmm):

bash · try it yourself
touch -d '2026-01-15 09:30' january.log
touch -t 202602201745 february.log
stat -c '%y %n' january.log february.log
2026-01-15 09:30:00.000000000 +0000 january.log
2026-02-20 17:45:00.000000000 +0000 february.log
Option Effect
-d 'DATE' Use this date instead of now
-t STAMP Same, in YYYYMMDDhhmm form
-r ref Copy the times of another file
-m / -a Change only the modification / access time
-c Don't create the file if it doesn't exist

Comparing file ages

Bash's test -nt (newer than) and -ot (older than) compare modification times. Together with touch this is the logic behind "rebuild only if the source changed":

bash · try it yourself
touch -d '2026-03-01 10:00' main.c
touch -d '2026-03-01 09:00' main.o
if [ main.c -nt main.o ]; then echo "main.o is stale: recompile"; fi
touch -r main.c main.o
if [ main.c -nt main.o ]; then echo "still stale"; else echo "main.o is up to date"; fi
ls -t main.c main.o
main.o is stale: recompile
main.o is up to date
main.c
main.o

touch -r main.c main.o copied main.c's times to main.o, so neither is newer.

Marker files

Scripts often use empty files as flags: a .done file marks a finished step, a lock file prevents two copies from running at once. touch -c updates a marker only if it already exists:

bash · try it yourself
touch -c missing.flag
ls missing.flag 2>/dev/null || echo "touch -c did not create missing.flag"
[ -e setup.done ] || { echo "running setup..."; touch setup.done; }
[ -e setup.done ] || { echo "running setup..."; touch setup.done; }
echo "setup ran once"
touch -c did not create missing.flag
running setup...
setup ran once

Test yourself

What happens to an existing file's content when you touch it?

  1. It is emptied
  2. Nothing; only its timestamps change
  3. A copy is created
  4. It is deleted

Which command gives b.txt the same timestamps as a.txt?

  1. touch a.txt b.txt
  2. touch -r a.txt b.txt
  3. cp a.txt b.txt
  4. touch -c b.txt

Which timestamp changes when you edit a file's content?

  1. Only ctime
  2. Only atime
  3. mtime (and ctime)
  4. None