Linux touch command: Create an empty file or change the file time.
Linux touch command Function Description
Use the touch command to create empty files and change the time of the files (atime and mtime).
The touch command has two functions:
- the current time of the system (the default way), their data will be left intact.
- The second is used to create new empty files.
Linux touch command Syntax
touch [Option] [File]
The meaning of each option in the command is shown in the table:
Option | Description |
---|---|
-a |
Change only the access time (atime) |
-m |
Change the file modification time record (mtime) |
-c |
If the target file does not exist, no new file is created |
-r<File> |
Uses the specified file’s time property instead of the current time |
-d<String> |
Uses the specified string to represent time instead of the current time |
-t<DateTime> |
Use the time in [[CC]YY]MMDDhhmm[.ss] format instead of the current time |
Linux touch command Examples
Create empty file apidemos-touch.txt
touch apidemos-touch.txt
Output:
Change the time record of file1 to 19:30 on September 17
[root@rhel ~]# touch -c -t 09171930 /root/file1
Set the access/modification time (atime and mtime) of the /root/file4 file to the same as the /etc/passwd file
[root@rhel ~]# touch -r /etc/passwd /root/file4
Batch file creation
touch file{1..5}.txt