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
Example 1 – Create a new file:
Command:
$ touch file.txt
Explanation:
- If
file.txtdoes not appear in the directory,touchcreates an empty file. - If
file.txtalready 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:
- -a: Updates just the file’s access time, leaving modification time unchanged.
- -m: Updates only the modification time, not access time.
- -c: Skips file creation if it does not exist.
- -t: Set custom date & time instead of system time.
Example -a:
$ touch -a file.txt
Output before and after:
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 -m:
$ 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 -c:
$ touch -c file_not_exist.txt
Output:
(no output)
Example -t:
$ touch -t 202509091230.45 file.txt
Output:
-rw-r--r-- 1 user user 0 Sep 9 12:30 file.txt
