The touch Command

The touch command modifies a file's timestamps. If the file specified doesn't exist, an empty file with that name is created.

Examples

  • If file.txt exists, set all of its timestamps to the current system time. If file.txt doesn't exist, create an empty file with that name.

[root@academy F1]# ls
[root@academy F1]# touch file.txt
[root@academy F1]# ls
file.txt
[root@academy F1]# cat file.txt
[root@academy F1]#
  • If file.txt exists, set its times to the current system time. If it does not exist, do nothing.

[root@academy F1]# stat file.txt
  File: ‘file.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 17214607    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2023-12-22 16:59:08.967907744 +0330
Modify: 2023-12-22 16:59:08.967907744 +0330
Change: 2023-12-22 16:59:08.967907744 +0330
 Birth: -
[root@academy F1]# touch -c file.txt
[root@academy F1]# stat file.txt
  File: ‘file.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 17214607    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2023-12-22 17:01:49.269906649 +0330
Modify: 2023-12-22 17:01:49.269906649 +0330
Change: 2023-12-22 17:01:49.269906649 +0330
 Birth: -
[root@academy F1]#
  • Change the access time of file.txt. The modification time is not changed. The change time is set to the current system time. If file.txt does not exist, it is created.

  • Change the access and modification times of file-b.txt to match the times of file-a.txt. The change time will be set to the current system time. If file-b.txt does not exist, it is not created. Note, file-a.txt must already exist in this context.

  • Set the access time and modification time of file.txt to February 1st of the current year. The change time is set to the current system time.

Last updated