The touch command

Linux

The primarily use of the Linux touch command is to update or modify the timestamp of files or directories.if the file does not exist the touch command will create it as an empty file.Touch is commonly used for file creation while it's not its original goal.

1. Create one or more empty files

touch file1 file2 file3

gives :

ls -l
total 0
-rw-rw-r-- 1 sc sc 0 juil. 20 14:28 file1
-rw-rw-r-- 1 sc sc 0 juil. 20 14:28 file2
-rw-rw-r-- 1 sc sc 0 juil. 20 14:28 file3

2. Update access and modification times

touch file 1

access and modification time are updated :

ls -l
total 0
-rw-rw-r-- 1 sc sc 0 juil. 20 14:41 file1
-rw-rw-r-- 1 sc sc 0 juil. 20 14:28 file2
-rw-rw-r-- 1 sc sc 0 juil. 20 14:28 file3

3. Update access or modification times

Using -a or -m options let us change access only or modification time only.

touch -a  file1

changes only access time :

ls -lu
total 0
-rw-rw-r-- 1 sc sc 0 juil. 20 14:44 file1
-rw-rw-r-- 1 sc sc 0 juil. 20 14:28 file2
-rw-rw-r-- 1 sc sc 0 juil. 20 14:28 file3

whereas -m updates modification time only

touch -m file1
 stat file1
  File: file1
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 259,4   Inode: 2910077     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/      sc)   Gid: ( 1000/      sc)
Access: 2025-07-20 14:49:26.160088175 +0100
Modify: 2025-07-20 14:49:25.422095884 +0100
Change: 2025-07-20 14:49:25.422095884 +0100
 Birth: 2025-07-20 14:28:08.811618076 +0100

4. Update timestamps of all files

touch *.txt 

will update the timestamps of all text files in the directory.

5. Update timestamp using a reference file

file1 is used as reference file

touch -r file1 file2

6. Create a file with a specified timestamp

touch -t [[CC]YY]MMDDhhmm[.ss] <filename>
  • CC - the first two digits for a year
  • YY - the last two digits for a year
  • MM - the month
  • DD - the day
  • hh - the hour
  • mm - the minutes
  • ss - the seconds

The digits in the square brackets are optional. When using the two-digit year format, setting YY to any number between 0-68 automatically assumes CC is 20, whereas 69-99 assumes CC is 19. Example :

touch -t 202507200915.30 file4

7. Set a timestamp using a date string

The -d option allows to use many human-readable forms :

  • Time of day, such as 9:30am,
  • Days of the week,
  • Calendar dates, such as "Sun, 29 Feb 2004 16:21:42 -0800"
  • Relative time, such as 6 months ago, yesterday, next Thursday etc.
touch -d yesterday file1

sources : man page phoenixNap vitux

Previous Post Next Post