Linux文件操作命令touch

时间:2022-10-09 11:21:20

touch

touch [filename]

创建文件,参数可以跟多个

如果要创建 50 个有规律的文件,例如 text1-text50

利用参数扩展

touch test{1..50}
touch test{a..e}
touch test{a..e}_{1..3}---> 会创建 a_1 a_2 a_3...

上帝之手,本来是用来修改文件时间戳的。文件的三个时间 ctime\mtime\atime

拓展内容:可以通过“stat”命令查看文件的三个时间

touch " " 可以放一些特殊字符

touch实验

[tom@rhel7 ~]$ touch test{a..c}_{1..4}
[tom@rhel7 ~]$ ls
testa_1 testa_4 testb_3 testc_2
testa_2 testb_1 testb_4 testc_3
testa_3 testb_2 testc_1 testc_4

--full-time可以查看mtime的完整时间

[tom@rhel7 ~]$ ls -l --full-time
total 0
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testa_1
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testa_2
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testa_3
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testa_4
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testb_1
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testb_2
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testb_3
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testb_4
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.854039544 +0800 testc_1
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.854039544 +0800 testc_2
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.854039544 +0800 testc_3
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.854039544 +0800 testc_4

[tom@rhel7 ~]$ touch "ab cd"

[tom@rhel7 ~]$ ls -b
ab\ \ \ cd testa_3 testb_2 testc_1 testc_4
testa_1 testa_4 testb_3 testc_2
testa_2 testb_1 testb_4 testc_3

touch拓展实验

[booboo@rhel7 ~]$ touch booboo
[booboo@rhel7 ~]$ ll
total 0
-rw-rw-r--. 1 booboo booboo 0 Jun 15 23:28 booboo
[booboo@rhel7 ~]$ stat booboo
  File: ‘booboo’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d	Inode: 143         Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1001/  booboo)   Gid: ( 1001/  booboo)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-06-15 23:28:55.041578819 -0400     #atime 文件最近一次被访问的时间
Modify: 2016-06-15 23:28:55.041578819 -0400    #mtime 文件内容最近一次修改的时间
Change: 2016-06-15 23:28:55.041578819 -0400    #ctime 文件属性最近一次修改的时间
 Birth: -

使用cat去访问booboo文件,可以发现atime被修改了

[booboo@rhel7 ~]$ cat booboo
[booboo@rhel7 ~]$ stat booboo
  File: ‘booboo’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d	Inode: 143         Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1001/  booboo)   Gid: ( 1001/  booboo)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-06-15 23:32:35.898724748 -0400
Modify: 2016-06-15 23:28:55.041578819 -0400
Change: 2016-06-15 23:28:55.041578819 -0400
 Birth: -

通过chmod修改文件权限后,会看到ctime时间改变,通过ll命令看到的时间为mtime

[booboo@rhel7 ~]$ chmod 777 booboo
[booboo@rhel7 ~]$ ll
total 0
-rwxrwxrwx. 1 booboo booboo 0 Jun 15 23:28 booboo  
[booboo@rhel7 ~]$ stat booboo
  File: ‘booboo’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d	Inode: 143         Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1001/  booboo)   Gid: ( 1001/  booboo)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-06-15 23:32:35.898724748 -0400
Modify: 2016-06-15 23:28:55.041578819 -0400
Change: 2016-06-15 23:33:49.195445761 -0400
 Birth: -

通过echo命令向booboo文件追加一些内容,会看到mtime时间变了,并且ctime也变了,思考为什么?

[booboo@rhel7 ~]$ echo hi >> booboo
[booboo@rhel7 ~]$ ll
total 4
-rwxrwxrwx. 1 booboo booboo 3 Jun 15 23:34 booboo
[booboo@rhel7 ~]$ stat booboo
  File: ‘booboo’
  Size: 3         	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 143         Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1001/  booboo)   Gid: ( 1001/  booboo)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-06-15 23:32:35.898724748 -0400
Modify: 2016-06-15 23:34:53.251332183 -0400
Change: 2016-06-15 23:34:53.251332183 -0400
 Birth: -