Skip to content

Linux Foundations / Find your way around Linux ยท Step 16 of 63

Lesson: touchSwitch
โ† Linux Foundations

Shell and files

touch

Create empty files and update file timestamps with the touch command.

touch has two jobs. Its real purpose is to update a file's timestamps. Its handy side effect is that it creates an empty file if the name does not exist yet.

Creating empty files

$ touch notes.txt
$ ls -l notes.txt
-rw-r--r-- 1 pete pete 0 Nov 29 10:00 notes.txt

The size is 0 because nothing was written. You can name several files at once:

$ touch a.txt b.txt c.txt

The directory must already exist:

$ touch drafts/plan.txt
touch: cannot touch 'drafts/plan.txt': No such file or directory

Existing files are safe

If the file exists, touch leaves its contents alone and sets its access and modification times to now:

$ ls -l report.txt
-rw-r--r-- 1 pete pete 52 Jan  5  2025 report.txt
$ touch report.txt
$ ls -l report.txt
-rw-r--r-- 1 pete pete 52 Nov 29 10:01 report.txt

The size stays at 52. This is useful for tools that decide what to rebuild or back up by looking at modification times.

Choosing what to change

  • -c does not create missing files.
  • -a changes only the access time.
  • -m changes only the modification time.
$ touch -c missing.txt
$ ls missing.txt
ls: cannot access 'missing.txt': No such file or directory

Setting a specific time

-t takes a stamp in the form [[CC]YY]MMDDhhmm[.ss]:

$ touch -t 202601151430 report.txt

That means 15 January 2026 at 14:30. GNU touch on Linux also accepts a readable date with -d:

$ touch -d "2026-01-15 14:30" report.txt

The forms accepted by -d differ on BSD and macOS. Another option, -r, copies the times from a reference file: touch -r old.txt new.txt.

Use stat report.txt to see all the timestamps. You can practise on files in the files and links lab.

Try it

  1. Create three empty files with one command.
  2. Check their sizes with ls -l.
  3. Set one to an old date with -t.
  4. Run plain touch on it and watch the date change.

Check your understanding

touch

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.