Linuxstar
Published on

touch command-create file

Jonas

Jonas

2 min read

The touch command has two functions: one is to create a new empty file, and the other is to change the timestamp attribute of an existing file.

The touch command will update the access time and modification time of the specified file according to the current system time. If the file does not exist, a new empty file will be created, unless the "-c" or "-h" option is specified.

Note: When modifying the time attribute of a file, the user must be the owner of the file or have the access right to write the file.

Syntax:

touch [parameter] [file]
Command parameters:
-aChange file reading time record
-mChange the modification time record of the file
-rUse the time record of the reference file, the effect is the same as -file
-cDo not create new files
-dSet the time and date, you can use a variety of different formats
-tSet the time record of the file, the format is the same as the date command
–no-createDo not create new files
–helpDisplay help information
–versionList version information

Example

Create an empty file:

[root@linuxstar ~]# touch file.txt

Create files in batches:

[root@linuxstar ~]# touch file{1..5}.txt 
[root@linuxstar ~]# lsfile1.txt file2.txt file3.txt file4.txt file5.txt

Modify file access (access) time:

[root@linuxstar ~]# stat file.txt   
File: file.txt   
Size: 0
Blocks: 0
IO Block: 512
regular empty file Device: 2h/2d
Inode: 87538717757135273
Links: 1 Access: (0666/-rw-rw-rw-)
Uid: ( 1000/  root)  
Gid: ( 1000/  root) 
Access: 2019-04-07 00:25:34.729216500 +0800 
Modify: 2019-04-07 00:25:34.729216500 +0800 
Change: 2019-04-07 00:25:34.729216500 +0800  
Birth: -
[root@linuxstar ~]# touch -a file.txt 
[root@linuxstar ~]# stat file.txt   
File: file.txt   Size: 0               
Blocks: 0          
IO Block: 512    
regular empty file Device: 2h/2d   
Inode: 87538717757135273  
Links: 1 Access: (0666/-rw-rw-rw-)  
Uid: ( 1000/  root)   
Gid: ( 1000/  root) 
Access: 2019-04-07 00:26:13.004116000 +0800 
Modify: 2019-04-07 00:25:34.729216500 +0800 
Change: 2019-04-07 00:26:13.004116000 +0800  
Birth: -