Touch is used to create a file, which is used to modify the timestamp of the file.

The command format

Touch [options]... The file...

The command parameter

  • -A or –time=atime or –time=access or –time=use changes only the access time.
  • -c or –no-create Creates no document.
  • -d Uses the specified date and time instead of the current time.
  • -f This parameter is ignored and only resolves compatibility problems with the BSD touch command.
  • -m or –time=mtime or –time=modify Only changes the change time.
  • -r Sets the date and time of the specified document or directory to the same as that of the reference document or directory.
  • -t Uses the specified date and time instead of the current time.

Command function

Touch command parameters can change the date and time of a document or directory, including access time and change time.

Create a directory that does not exist

Create a 1.txt file > touch 1.txt and create 2.txt 3. TXT file > touch 2.txt 3. TXTCopy the code

Will be 5. TXTAccess.ModifyChange the time to the same as 1.txt

> touch -r 1.txt 5.txt
> ls 
-rw-r--r-- 1 root root 0 Feb  3 23:17 1.txt
-rw-r--r-- 1 root root 0 Feb  3 23:17 5.txt
Copy the code

Batch create regular files

TXT file2.txt…. file10.txt

> touch file{1.. 10}.txtCopy the code

Create a file and specify a timestamp for the file

> touch -t 202102031111 3.txt
> ls -al
-rw-r--r-- 1 root root 0 Feb  3 11:11 3.txt
Copy the code

Change the time of 5.txt to 2 days ago

> ls -al 5.txt -rw-r--r-- 1 root root 0 Feb 3 23:17 5.txt > touch -d "2 days ago" 5.txt > ls -al 5.txt > ls -rw-r--r-- 1  root root 0 Feb 1 23:29 5.txtCopy the code

Only change the1.txttheModifyandChangeThe time of

> stat 1.txt File: '1.txt' Size: 5 Blocks: 8 IO Block: 4096 Regular File Device: fd01h/64769d Inode: 101371574 Links: 1 Access: (0644/-rw-r--r--) Uid: (0 / root) Gid: (0 / root) Access: 2021-02-03 23:39:45.258947600 +0800 Modify: 2021-02-03 23:40:10.462066771 +0800 Change: 2021-02-03 23:40:10.462066771 +0800 Birth: TXT File: '1.txt' Size: 5 Blocks: 8 IO Block: 4096 Regular File Device: fd01h/64769d Inode: 101371574 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2021-02-03 23:39:45.258947600 +0800 Modify: 2021-02-03 23:40:53.068649293 +0800 Change: 2021-02-03 23:40:53.068649293 +0800 Birth: -Copy the code

Why does Linux create files with Touch instead of Create

Touch - Change File Access and modification Times (BSD) touch - change file timestamps (GNU)Copy the code

Instead of creating a file, touch sets the modification time of a specified file to the current time. You pretend to “touch” the file, pretend it has been “modified,” and the modification time of the file is set to the current time. A side effect of this is that when you touch a file that doesn’t exist, it creates the file. Then, since Touch is ready to create files, there is no need for a separate CREATE.

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station