The touch Command

Yaser Rahmati | یاسر رحمتی

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.

[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:47:23.170912561 +0330
Modify: 2023-12-22 16:47:23.170912561 +0330
Change: 2023-12-22 16:47:23.170912561 +0330
 Birth: -
[root@academy F1]# touch -a 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 16:56:01.079909026 +0330
Modify: 2023-12-22 16:47:23.170912561 +0330
Change: 2023-12-22 16:56:01.079909026 +0330
 Birth: -
[root@academy F1]#
  • 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.

[root@academy F1]# touch file-a.txt
[root@academy F1]# stat file-a.txt
  File: ‘file-a.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:23:24.414897809 +0330
Modify: 2023-12-22 17:23:24.414897809 +0330
Change: 2023-12-22 17:23:24.414897809 +0330
 Birth: -
[root@academy F1]# touch file-b.txt
[root@academy F1]# stat file-b.txt
  File: ‘file-b.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 17214500    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:25:05.548897119 +0330
Modify: 2023-12-22 17:25:05.548897119 +0330
Change: 2023-12-22 17:25:05.548897119 +0330
 Birth: -
[root@academy F1]# touch -cr file-a.txt file-b.txt
[root@academy F1]# stat file-b.txt
  File: ‘file-b.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 17214500    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:23:24.414897809 +0330
Modify: 2023-12-22 17:23:24.414897809 +0330
Change: 2023-12-22 17:25:42.319896868 +0330
 Birth: -
[root@academy F1]#
  • 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.

[root@academy F1]# touch 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:28:29.508895727 +0330
Modify: 2023-12-22 17:28:29.508895727 +0330
Change: 2023-12-22 17:28:29.508895727 +0330
 Birth: -
[root@academy F1]# touch -d "1 Feb" 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-02-01 00:00:00.000000000 +0330
Modify: 2023-02-01 00:00:00.000000000 +0330
Change: 2023-12-22 17:28:41.190895647 +0330
 Birth: -
[root@academy F1]#

Last updated