Linux touch Command
The Linux touch command is used to modify the time attributes of files or directories, including access and modification times. If the file does not exist, the system will create a new file.
The ls -l
command can display the time records of the file.
Syntax
touch [-acfm][-d<date-time>][-r<reference-file-or-directory>] [-t<date-time>][--help][--version][file-or-directory…]
Parameter Description:
- a Change the access time record of the file.
- m Change the modification time record of the file.
- c If the target file does not exist, a new file will not be created. This has the same effect as --no-create.
- f Not used, retained for compatibility with other Unix systems.
- r Use the time record of the reference file, same effect as --file.
- d Set the date and time, which can be in various formats.
- t Set the time record of the file, in the same format as the date command.
- --no-create Will not create a new file.
- --help Display the command format.
- --version Display version information.
Example
Use the "touch" command to modify the time attributes of the file "testfile" to the current system time, enter the following command:
$ touch testfile # Modify the file's time attributes
First, use the ls command to view the attributes of the testfile file, as shown below:
$ ls -l testfile # View the file's time attributes
# Original file modification time was 16:09
-rw-r--r-- 1 hdd hdd 55 2011-08-22 16:09 testfile
After executing the "touch" command to modify the file attributes and then viewing the file's time attributes again, as shown below:
$ touch testfile # Modify the file's time attributes to the current system time
$ ls -l testfile # View the file's time attributes
# Modified file's time attributes to the current system time
-rw-r--r-- 1 hdd hdd 55 2011-08-22 19:53 testfile
When using the "touch" command, if the specified file does not exist, a new blank file will be created. For example, to create a new blank file named "file" in the current directory, enter the following command:
$ touch file # Create a new blank file named "file"