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
touch notes.txt
touch index.html style.css app.js
ls
stat -c '%s bytes: %n' notes.txtapp.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:
echo "important data" > keep.txt
touch keep.txt
cat keep.txt
> keep.txt
stat -c '%s bytes after > keep.txt' keep.txtimportant data
0 bytes after > keep.txt
Brace expansion: many files at once
touch chapter-{1..5}.md
touch test_{api,db,ui}.py
lschapter-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):
touch -d '2026-01-15 09:30' january.log
touch -t 202602201745 february.log
stat -c '%y %n' january.log february.log2026-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":
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.omain.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:
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?
- It is emptied
- Nothing; only its timestamps change
- A copy is created
- It is deleted
Which command gives b.txt the same timestamps as a.txt?
- touch a.txt b.txt
- touch -r a.txt b.txt
- cp a.txt b.txt
- touch -c b.txt
Which timestamp changes when you edit a file's content?
- Only ctime
- Only atime
- mtime (and ctime)
- None