Linux for Developers Course
Linux for Developers
/
Beginner

touch

Definition

A command primarily used to create empty files. Its secondary (and original) purpose is to update the access and modification timestamps of an existing file.

Explain Like I'm New

The 'create a blank file' command. It instantly creates a file with zero text inside it.

Real World Example

You want to write a Python script. You type `touch script.py`. An empty file is instantly created, which you can now open in a code editor.

Common Use Cases

  • •Creating blank configuration files
  • •Triggering file watchers by updating timestamps

Crucial Command Flags

Flag / OptionDescription
-cNo create. Do not create any files if they don't exist.
-mChange only the modification time.
-d [String]Parse string and use it instead of current time.

Interview Questions

basic

  • What happens if you use `touch` on a file that already exists and is full of text? Does it delete the text?

intermediate

  • Why is it called 'touch' instead of 'create'?

Flash Cards

Question

Does it delete text?

Click to reveal answer
Answer

No. The file's contents are perfectly safe. `touch` simply updates the file's 'Last Modified' timestamp to the current clock time.

Question

Why called touch?

Click to reveal answer
Answer

Because its original purpose in Unix was simply to 'touch' a file to update its timestamp. Creating a blank file was just a side-effect if the file didn't already exist, but it became the most popular way to use it.