Read

Module 2 · Files and directories

Creating empty files with touch

What touch was actually designed for - updating timestamps - and why it doubles as the quickest way to make an empty file, with -d, -c and -r along the way.

What you will learn

  • Create one or several empty files.
  • Understand modification time and update it with `touch`.
  • Set a specific timestamp with `-d` and copy one with `-r`.

You have been using touch since lesson 1 to create files. That is its most common use today, but a side effect of its real job. Every file carries a modification time — the moment its contents last changed, the date ls -l shows — and touch sets that time to *now*. If the file does not exist, it has to be created first so there is something to stamp. Hence the empty file.

~% cd lab/l12
l12% touch todo.txt
l12% touch a.log b.log c.log
l12% ls -l
total 0
-rw-r--r--    1 root     root             0 Oct  4 19:02 a.log
-rw-r--r--    1 root     root             0 Oct  4 19:02 b.log
-rw-r--r--    1 root     root             0 Oct  4 19:02 c.log
-rw-r--r--    1 root     root             0 Oct  4 19:02 todo.txt

Size 0, today's date. Several names make several files. Existing files are not emptied or changed in any way — only their timestamp moves — so touch is always safe to run on something that already exists. That is a real difference from editors and from > redirection (lesson 22), both of which can destroy contents.

Why anyone would update a timestamp

Build tools such as make decide what to recompile by comparing dates: if a source is newer than its output, rebuild. Touching a file forces that. Backup and sync tools pick files by age. find -newer and find -mtime (lesson 36) select files by date, and ls -t sorts by it. Marking "I looked at this today" or staging a realistic test scenario are everyday reasons to touch a file that already exists.

Choosing the time: -d, -t, -r

OptionSets the time toExample
(none)Now.touch report.txt
-d DATEA date, optionally with a time.touch -d 2020-01-01 old.txt, touch -d '2021-06-15 12:00' x
-t STAMPThe same, in the compact YYYYMMDDhhmm form.touch -t 202001010000 old.txt
-r FILEWhatever time FILE has.touch -r original.txt copy.txt
-c(modifier) do not create the file if it is missing.touch -c maybe.txt
l12% touch -d 2019-03-10 stale.txt
l12% ls -l stale.txt
-rw-r--r--    1 root     root             0 Mar 10  2019 stale.txt
l12% touch stale.txt
l12% ls -l stale.txt
-rw-r--r--    1 root     root             0 Oct  4 19:03 stale.txt

Notice how ls -l shows the year for the 2019 file and the clock time for today's. That is ls saving space: entries older than about six months get the year instead of the time. The data behind both is the same full timestamp; ls -l --full-time shows it completely.

Commands in this lesson

CommandWhat it does
touch FILECreate FILE empty, or set its modification time to now.
touch A B CSeveral files at once.
touch -d DATE FILEStamp FILE with a given date.
touch -r REF FILECopy REF's timestamp onto FILE.
touch -c FILEUpdate the time only if FILE exists.

Quiz

  1. What happens when you `touch` a file that already has content?

    • Its content is erased
    • Only its modification time changes
    • An error: the file exists
  2. Which command creates `a.txt`, `b.txt` and `c.txt` in one go?

    • `touch a.txt, b.txt, c.txt`
    • `touch a.txt b.txt c.txt`
    • `touch -3 a b c .txt`
  3. What is `touch` originally for?

    • Creating files
    • Updating a file's timestamp
    • Checking whether a file exists
  4. Which option stops `touch` from creating a missing file?

    • `-c`
    • `-d`
    • `-r`
  5. `ls -l` shows `Mar 10 2019` for one file and `Oct 4 19:03` for another. Why the different formats?

    • The first file has no time recorded
    • `ls` shows the year instead of the time for files older than about six months
    • One file is a directory

Practice

  1. In /root/lab/l12, create an empty file called `todo.txt`.

  2. Create three empty files — `a.log`, `b.log` and `c.log` — inside /root/lab/l12/logs with a single command.

  3. /root/lab/l12/stale.txt is dated 2019. Bring its modification time up to now without changing anything else.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.