Linux touch command

|

What is the touch Command?

The touch command is used to create a new empty file in Linux. It can also update the last accessed and modified time of an existing file without changing its content.

What Happens When You Use the touch Command on an Existing File?

When you use the touch command on a file that already exists, the file remains unchanged. It only updates its last accessed and modified time. It does not create a new file.

Syntax:

touch [option] filename

Example 1: Create a new file


Command:

$ touch file.txt

Explanation:

  • If file.txt does not appear in the directory, touch creates an empty file.
  • If file.txt already exists, it only updates its modified and access time.

Example 2: Create multiple files


Command:

$ touch file1.txt file2.txt file3.txt

Explanation:

This command creates three new files or updates timestamps if they already exist.


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
-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

Options:

  1. -a: Updates just the file's access time, leaving modification time unchanged.
  2. -m: Updates only the modification time, not access time.
  3. -c: Skips file creation if it does not exist.
  4. -t: Set custom date & time instead of system time.

Example 1: -a Option


Command:

$ touch -a file.txt

Output:

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

Example 2: -m Option


Command:

$ touch -m file.txt

Output:

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

Example 3: -c Option


Command:

$ touch -c file_not_exist.txt

Output:

(no output)

Example 4: -t Option


Command:

$ touch -t 202509091230.45 file.txt

Output:

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

ONLINE WEB TOOLS

WhatIsMyIpAddress
Shorterner