Account
Categories

touch command


Definition:

In the terminal, when you use the touch command on a file that already exists, it remains unchanged.

If the file is already present, touch only updates its last accessed and modified time—it does not create a new file.

Syntax:

touch [option] filename

Options:

  • -a: Updates just the file’s access time, leaving the modification time unchanged.
  • -m: Updates only the modification time of the file, not the access time.
  • -c: If the file does not exist, it will not create a new file; it just skips.
  • -t: You can give the file your own date and time instead of the system’s current time.

Example 1 – Create a new file

Command:

touch file.txt

Explanation: If file.txt does not exist, this command creates an empty file. If it exists, it updates its access/modified time.

Output:

$ ls -l
-rw-r--r-- 1 user group 0 Sep 9 12:30 file.txt

Example 2 – Create multiple files

Command:

touch file1.txt file2.txt file3.txt

Explanation: Creates multiple files at once or updates their timestamps if they already exist.

Output:

$ ls -l
-rw-r--r-- 1 user group 0 Sep 9 12:30 file1.txt
-rw-r--r-- 1 user group 0 Sep 9 12:30 file2.txt
-rw-r--r-- 1 user group 0 Sep 9 12:30 file3.txt

Example 3 – Change date & time

Command:

touch -t 202509091230 file.txt

Explanation: Sets the file’s last modified/access time to 2025-09-09 12:30.

Output:

$ ls -l
-rw-r--r-- 1 user group 0 Sep 9 12:30 file.txt

Option Examples:

touch -a file.txt

Before: -rw-r--r-- 1 user user 0 Sep 8 10:00 file.txt
After:  -rw-r--r-- 1 user user 0 Sep 8 11:15 file.txt

touch -m file.txt

Before: -rw-r--r-- 1 user user 0 Sep 8 10:00 file.txt
After:  -rw-r--r-- 1 user user 0 Sep 8 11:20 file.txt

touch -c file_not_exist.txt

(no output)

touch -t 202509091230.45 file.txt

-rw-r--r-- 1 user user 0 Sep 9 12:30 file.txt