Linux touch commands detail

touch command

touch command has two functions: one for the existing file time stamp is updated to the current system time (default), their data will be preserved intact; the second is used to create a new, empty file.

 grammar

touch (options) (parameters)

The most common usage: touch fileA
- if fileA exist, use the touch command to change the date and time the file or directory, including access time and change the time;
- if fileA does not exist, touch will create a new command in the current directory a blank file fileA .

 parameter
parameter Explanation
-a  Change only the access time.
-c  Does not create any documents.
-d Specified date and time, not time now. [[CC]YY]MMDD text
-f This argument ignores not be processed, only responsible for resolving compatibility issues BSD version of the touch command.
-m  Change only change time.
-r The date and time the document or directory specified, all set to the date and time reference of the same document or directory.
-t Specified date and time, not time now[CC[YY]MMDDhhmm[.SS] 
--help Online Help
--version Displays version information.
 Use examples:
 Example one: create a file that does not exist

touch test1.txt test2.txt

[root@Linuxidc tmp]# touch test1.txt test2.txt
[root@Linuxidc tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 30 14:54 test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:54 test2.txt
 Example 2: If test3 does not exist, no file is created

touch -c test3.txt

[root@Linuxidc tmp]# touch -c test3.txt
[root@Linuxidc tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 30 14:54 test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:54 test2.txt
 Example Three: test1.txt update time stamp and the same test2.txt

touch touch test1.txt test3.txt

[root@Linuxidc tmp]# ll test1.txt test3.txt
-rw-r--r-- 1 root root 0 Nov 30 14:54 test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:58 test3.txt
[root@Linuxidc tmp]# touch test1.txt test3.txt
[root@Linuxidc tmp]# ll test1.txt test3.txt
-rw-r--r-- 1 root root 0 Nov 30 14:58 test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:58 test3.txt
[root@Linuxidc tmp]#
 Time stamp of the configuration file: Four examples

touch -t 201808080808 test1.txt

[root@Linuxidc tmp]# ll test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:58 test1.txt
[root@Linuxidc tmp]# touch -t 201808080808 test1.txt

[root@Linuxidc tmp]# stat test1.txt
  File: 'test1.txt'
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d    Inode: 11041       Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-08-08 08:08:00.000000000 +0800
Modify: 2018-08-08 08:08:00.000000000 +0800
Change: 2019-05-30 15:01:03.959402702 +0800
 Birth: -
 Example Five: later time to the time preceding the time to test2.txt time of test1.txt.

touch -r test1.txt test2.txt 

[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 8月   8 2018 test1.txt
-rw-r--r-- 1 root root 0 11月 30 14:54 test2.txt
-rw-r--r-- 1 root root 0 11月 30 14:58 test3.txt
[root@Linuxidc tmp]# touch -r test1.txt test2.txt
[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 8月   8 2018 test1.txt
-rw-r--r-- 1 root root 0 8月   8 2018 test2.txt
-rw-r--r-- 1 root root 0 11月 30 14:58 test3.txt
 Examples of six: to modify so .txt file to October 13, 2013 time. Operation command:

touch -d "10/13/2013" *.txt

[root@Linuxidc tmp]# touch -d 20161013 *.txt
[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月 13 00:00 test1.txt
-rw-r--r-- 1 root root 0 10月 13 00:00 test2.txt
-rw-r--r-- 1 root root 0 10月 13 00:00 test3.txt

Further modifications may be separate time or the month, as follows:
in a format am, pm or 24 hours, can use other formats, such as date 6 May 2000.
- Touch -d "6:03 PM" File
- Touch -d "05/06/2000" File
- Touch -d "05/06/2000 6:03 PM" File


 Run touch without any options: Example seven

The easiest to use touch commands. Just type: $ touch file_name
Please observe the following a screenshot.

[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月 13 00:00 test1.txt
[root@Linuxidc tmp]# touch test1.txt
[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 11月 30 15:23 test1.txt

test1.txt original time stamp is 00:00 after we use the touch command, it changed to 15:23. This shows that, by default, the time stamp of the file will be changed to touch the current time.

 Examples of Eight: only change the access time

As we mentioned before, each file is accompanied by access time and modification time. The above time stamp is 15:23. We can see more details.

[root@Linuxidc tmp]# stat test1.txt
  文件:"test1.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11041       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2019-05-30 15:23:27.216913900 +0800
最近更改:2019-05-30 15:23:27.216913900 +0800
最近改动:2019-05-30 15:23:27.216913900 +0800
创建时间:-

We found that the value of access time and modification time is 15:23:27 is the same, and they belong to the same time zone GMT +8.
If we now want to change only the access time, we need to use the -a option.
touch -a test1.txt

[root@Linuxidc tmp]# touch -a test1.txt
[root@Linuxidc tmp]# stat test1.txt
  文件:"test1.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11041       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2019-05-30 15:28:35.938248963 +0800
最近更改:2019-05-30 15:26:16.850686494 +0800
最近改动:2019-05-30 15:28:35.938248963 +0800
创建时间:-

As you can see, the access time becomes a 15:28:35, but modification time is still 15:26:16

 Examples nine: only change the modification time

Use -m options to achieve
touch -m test1.txt

[root@Linuxidc tmp]# touch -m test1.txt
[root@Linuxidc tmp]# stat test1.txt
  文件:"test1.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11041       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2019-05-30 15:28:35.938248963 +0800
最近更改:2019-05-30 15:31:41.033247158 +0800
最近改动:2019-05-30 15:31:41.033247158 +0800
创建时间:-

Now modification time changed to 15:31:41. Note that when a file is accessed or modified, the state will always change the value of its time-domain record.

 Examples ten: Change to a custom timestamp

-a and -m option will change the file timestamp is now the current time, you can also change the custom stamp. Use -t option to achieve.
From the example above example test1.txt, we see that it is a timestamp:
- recent visit: 2019-05-30 15:28:35
- Recent changes: 2019-05-30 15:31:41
- Recent changes : 2019-05-30 15:31:41

If we want to access and modification time was changed to January 12 2017 09:58:27 . We can use the following command to complete:
 touch -t 201701120958.27 test1.txt

[root@Linuxidc tmp]# touch -t 201701120958 test1.txt
[root@Linuxidc tmp]# stat test1.txt
  文件:"test1.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11041       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2017-01-12 09:58:00.000000000 +0800
最近更改:2017-01-12 09:58:00.000000000 +0800
最近改动:2019-05-30 15:36:43.837822257 +0800
创建时间:-

-t  option syntax consists of:

[[CC]YY]MMDDhhmm [.SS]

CC - the first two years  YY - last two digits of the year
MM - the month [01-12] DD - date [01-31]
hh - when [00-23] mm - min [00-59]
SS - s [00-61]  

Here, CC is the number of years in the first two, namely "centuries"; YY is the last two digits of the year, that is, several years in a century. If not given value CC, the number of touch will CCYY defined within the 1969--2068. MM is the number of the month, DD for the day will be limited in the number of years CCYY of 1969--2068. MM is the number of the month, DD is the number of days, hh is the number of (several) hours, mm is the number of minutes, SS is the number of seconds. Second setting range is 0--61 here, so leap seconds can be processed. Time for these numbers is a time TZ environment variable specified time zone. Due to system constraints, time is earlier than January 1, 1970 is wrong.

 Example XI: Another way to change the date and time

If you think [[CC] YY] MMDDhhmm [ .SS] format does not suit you, we can also use the -d option. Here is -d an example of what options are used.
For example, we have a file called test3.txt, and its related properties demonstrated in the screenshot below.

[root@Linuxidc tmp]# stat test3.txt 
  文件:"test3.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11044       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2016-10-13 00:00:00.000000000 +0800
最近更改:2016-10-13 00:00:00.000000000 +0800
最近改动:2019-05-30 15:11:35.285472753 +0800
创建时间:-

Now we have to date of January 14, 2017 was changed to December 20, 2013 . You can use the following command:
$ touch -d '10-December-2013' test3.txt

[root@Linuxidc tmp]#  touch -d '10-December-2013' test3.txt
[root@Linuxidc tmp]# stat test3.txt 
  文件:"test3.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11044       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2013-12-10 00:00:00.000000000 +0800
最近更改:2013-12-10 00:00:00.000000000 +0800
最近改动:2019-05-30 16:22:21.647100822 +0800
创建时间:-

Not surprisingly, the date access and modification time has been changed to December 10, 2013.

 Examples 12: changing time zones

If we want to change the designation of the GMT time, we can also use the -d option.
touch file_3.txt

[root@Linuxidc tmp]# touch test3.txt 
[root@Linuxidc tmp]# stat test3.txt 
  文件:"test3.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11044       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2019-05-30 16:23:50.453099824 +0800
最近更改:2019-05-30 16:23:50.453099824 +0800
最近改动:2019-05-30 16:23:50.453099824 +0800
创建时间:-

Test3.txt difficult to find the time zone is GMT +0800. To change GMT3 time zone, we only need to type the following command:
touch -d GMT3 test3.txt

[root@Linuxidc tmp]# touch -d GMT3 test3.txt
[root@Linuxidc tmp]# stat test3.txt 
  文件:"test3.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11044       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2019-05-30 11:00:00.000000000 +0800
最近更改:2019-05-30 11:00:00.000000000 +0800
最近改动:2019-05-30 16:25:25.496878027 +0800
创建时间:-

Well, now the time has changed to 11:00:00 AM.

 Examples of thirteen: Use the -d option in conjunction with the next parameter

You can also use a cool way -d option. Please observe the following picture.

[root@Linuxidc tmp]# touch -d 'next tuesday GMT3' test3.txt 
[root@Linuxidc tmp]# stat test3.txt 
  文件:"test3.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11044       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2019-04-06 11:00:00.000000000 +0800
最近更改:2019-04-06 11:00:00.000000000 +0800
最近改动:2019-05-30 16:28:55.210247929 +0800
创建时间:-

We can words  next Sunday  into one value and GMT 3, while the touch command can still recognize it.

There is another -d example options. First of all, we want to file3_3.txt reset to the current date and time.
touch file_3.txt

[root@Linuxidc tmp]# touch test3.txt 
[root@Linuxidc tmp]# stat test3.txt 
  文件:"test3.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11044       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2019-05-30 16:30:36.173246795 +0800
最近更改:2019-05-30 16:30:36.173246795 +0800
最近改动:2019-05-30 16:30:36.173246795 +0800
创建时间:-

Then we try this command:
touch -d '1 year ago 13:43:07' test3.txt

[root@Linuxidc tmp]# touch -d '1 year ago 13:43:07' test3.txt
[root@Linuxidc tmp]# stat test3.txt 
  文件:"test3.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:11044       硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2018-11-30 13:43:07.000000000 +0800
最近更改:2018-11-30 13:43:07.000000000 +0800
最近改动:2019-05-30 16:31:30.369246185 +0800
创建时间:-

Too shocked, Touch even recognize the word ' 1 year ago Member '.

 Examples 14: Create an empty file

When you run the touch command target file does not exist, touch will create an empty file with the same name.
touch test4.txt

[root@Linuxidc tmp]# touch test4.txt
[root@Linuxidc tmp]# ll test4.txt
-rw-r--r-- 1 root root 0 11月 30 16:34 test4.txt
 Examples of five: create multiple files

You can use the target file space separated, in order to create multiple files.
touch doc_10.txt doc_20.txt doc_30.txt

[root@Linuxidc tmp]# touch doc_10.txt doc_20.txt doc_30.txt
[root@Linuxidc tmp]# ll doc*
-rw-r--r-- 1 root root 0 11月 30 16:35 doc_10.txt
-rw-r--r-- 1 root root 0 11月 30 16:35 doc_20.txt
-rw-r--r-- 1 root root 0 11月 30 16:35 doc_30.txt

Guess you like

Origin www.linuxidc.com/Linux/2019-06/158911.htm