"The touch command is used to create an empty file, or to change the timestamp of an existing file on UNIX and Linux systems. The change timestamp here means the access and modification time of updated files and directories.
Let's look at the syntax and options of the touch command:
Syntax:
#Touch {options} {files}
Options used in the touch command:
In this article, we will introduce nine useful touch command examples in Linux.
Example: 1 create an empty file using touch
To create an empty file using the touch command on a Linux system, type touch, and then enter the file name. As follows:
[ root@linuxtechi ~]# touch devops.txt
[ root@linuxtechi ~]# ls -l devops.txt
-rw-r--r--.1root root0Mar2922:39devops.txt
Example: 2 create a batch of empty files using touch
There may be some situations where we have to create a large number of empty files for some tests, which can be easily realized by using the touch command:
[ root@linuxtechi ~]# touch sysadm-{1..20}.txt
In the above example, we created 20 empty files named sysadm-1.txt to sysadm-20.txt. You can change the names and numbers as needed.
Example: 3 change / update the access time of files and directories
Suppose we want to change the access time of the file named devops.txt, use the - a option in the touch command, and then enter the file name. As follows:
[ root@linuxtechi ~]# touch -a devops.txt
Now use the stat command to verify that the access time of the file has been updated:
[ root@linuxtechi ~]# stat devops.txt
File: 'devops.txt'
Size: 0 Blocks: 0IO Block: 4096 regular empty file
Device: fd00h/64768dInode: 67324178Links: 1
Access: (0644/-rw-r--r--)Uid: (0/root) Gid: (0/root)
Context: unconfined_ u:object_ r:admin_ home_ t:s0
Access: 2018-03-2923:03:10.902000000 -0400
Modify: 2018-03-2922:39:29.365000000 -0400
Change: 2018-03-2923:03:10.902000000 -0400
Birth: -
Change the access time of the directory:
Suppose we have an nfsshare folder in the / MNT directory. Let's change the access time of this folder with the following command:
[ root@linuxtechi ~]# touch -m /mnt/nfsshare/
[ root@linuxtechi ~]# stat /mnt/nfsshare/
File: '/mnt/nfsshare/'
Size: 6Blocks: 0IO Block: 4096directory
Device: fd00h/64768dInode: 2258Links: 2
Access: (0755/drwxr-xr-x)Uid: (0/root)Gid: (0/root)
Context: unconfined_ u:object_ r:mnt_ t:s0
Access: 2018-03-2923:34:38.095000000 -0400
Modify: 2018-03-0310:42:45.194000000 -0500
Change: 2018-03-2923:34:38.095000000 -0400
Birth: -
Example: 4 change the access time without creating a new file
In some cases, if the file exists, we want to change the access time of the file and avoid creating the file. Use the - C option in the touch command. If the file exists, we can change the access time of the file. If it does not exist, we will not create it.
[ root@linuxtechi ~]# touch -c sysadm-20.txt
[ root@linuxtechi ~]# touch -c winadm-20.txt
[ root@linuxtechi ~]# ls -l winadm-20.txt
ls: cannot access winadm-20.txt: No such file ordirectory
Example: 5 change the modification time of files and directories
Using the - M option in the touch command, we can change the modification time of files and directories.
Let's change the change time of the file named devops.txt:
[ root@linuxtechi ~]# touch -m devops.txt
Now use the stat command to verify that the modification time has changed:
[ root@linuxtechi ~]# stat devops.txt
File: 'devops.txt'
Size: 0Blocks: 0IO Block: 4096regular empty file
Device: fd00h/64768dInode: 67324178Links: 1
Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)
Context: unconfined_ u:object_ r:admin_ home_ t:s0
Access: 2018-03-2923:03:10.902000000 -0400
Modify: 2018-03-2923:59:49.106000000 -0400
Change: 2018-03-2923:59:49.106000000 -0400
Birth: -
Similarly, we can change the modification time of a directory:
[ root@linuxtechi ~]# touch -m /mnt/nfsshare/
Use stat to cross verify access and modification times:
[ root@linuxtechi ~]# stat devops.txt
File: 'devops.txt'
Size: 0Blocks: 0IO Block: 4096regular empty file
Device: fd00h/64768dInode: 67324178Links: 1
Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)
Context: unconfined_ u:object_ r:admin_ home_ t:s0
Access: 2018-03-3000:06:20.145000000 -0400
Modify: 2018-03-3000:06:20.145000000 -0400
Change: 2018-03-3000:06:20.145000000 -0400
Birth: -
Example: 7 set the access and modification time to a specific date and time
Whenever we use the touch command to change the access and modification time of a file or directory, it sets the current time to the access and modification time of the file or directory.
Suppose we want to set a specific date and time as the access and modification time of the file, which can be achieved by using the - C and - t options in the touch command.
The date and time can be specified in the following format:
{CCYY}MMDDhhmm.ss
Of which:
CC – first two digits of the year
YY – the last two digits of the year
Mm – month (01-12)
DD – days (01-31)
HH – hours (00-23)
Mm – minutes (00-59)
Let's set the access and modification time of the devops.txt file to a future time (18:20 on October 19, 2025).
[ root@linuxtechi ~]# touch -c -t 202510191820 devops.txt
Use the stat command to view the update access and modification time:
Set the access and modification time according to the date string, use the - D option in the touch command, and then specify the date string followed by the file name. As follows:
[ root@linuxtechi ~]# touch -c -d ""2010-02-07 20:15:12.000000000 +0530"" sysadm-29.txt
Use the stat command to verify the status of the file:
[ root@linuxtechi ~]# stat sysadm-20.txt
File: ‘sysadm-20.txt’
Size: 0Blocks: 0IO Block: 4096regular empty file
Device: fd00h/64768dInode: 67324189Links: 1
Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)
Context: unconfined_ u:object_ r:admin_ home_ t:s0
Access: 2010-02-0720:15:12.000000000 +0530
Modify: 2010-02-0720:15:12.000000000 +0530
Change: 2018-03-3010:23:31.584000000 +0530
Birth: -
Note: in the above command, if we do not specify - C, if the file does not exist in the system, the touch command will create a new file and set the timestamp to the one given in the command.
Example: 8 use reference file to set timestamp (- R)
In the touch command, we can use the reference file to set the timestamp of the file or directory. Suppose I want to set the same timestamp on the devops.txt file as the sysadm-20.txt file, which can be easily implemented by using the - R option in the touch command.
Syntax:
#Touch - R {reference document} real document
[ root@linuxtechi ~]# touch -r sysadm-20.txt devops.txt
Example: 9 changing access and modification times on symbolic link files
By default, whenever we try to change the timestamp of a symbolic link file using the touch command, it only changes the timestamp of the original file. If you want to change the timestamp of the symbolic link file, you can use the - H option in the touch command.
Syntax:
#Touch - H {symbolic link file}
[ root@linuxtechi opt]# ls -l /root/linuxgeeks.txt
lrwxrwxrwx.1root root15Mar3010:56 /root/linuxgeeks.txt -> linuxadmins.txt
[ root@linuxtechi ~]# touch -t 203010191820 -h linuxgeeks.txt
[ root@linuxtechi ~]# ls -l linuxgeeks.txt
Lrwxrwxrwx.1root root15oct192030linuxgeeks.txt - > linuxadmins.txt, read the full text, original title: 9 useful touch command examples under Linux
The source of the article: [micro signal: LinuxHub, WeChat official account: Linux enthusiasts] welcome to add attention! Please indicate the source of the article“
Our other product: