【Linux学习】Linux必备命令(一)–之touch命令详解

1.命令详解

touch 命令主要用于创建普通文件,用法为 touch test.txt,如果文件存在,则表示修改当前文件时间,常用参数详解如下:

用法:touch [选项] 文件	将每个文件的访问时间和修改时间改为当前时间;
						不存在的文件将会被创建为空文件,除非使用-c 或-h 选项;
						如果文件名为"-"则特殊处理,更改与标准输出相关的文件的访问时间;
						长选项必须使用的参数对于短选项时也是必需使用的;
-a 						只更改访问时间;
-c, --no-create 		不创建任何文件;
-d, --date=字符串 		使用指定字符串表示时间而非当前时间;
-f						 (忽略)-h, --no-dereference 	会影响符号链接本身,而非符号链接所指示的目的地;
						(当系统支持更改符号链接的所有者时,此选项才有用)-m 						只更改修改时间;
-r, --reference=文件 	使用指定文件的时间属性而非当前时间;
-t STAMP 				使用[[CC]YY]MMDDhhmm[.ss] 格式的时间而非当前时间;
	--time=WORD 		使用 WORD 指定的时间:access、atime、use 都等于-a;
						选项的效果,而 modify、mtime 等于-m 选项的效果;
	--help 				显示此帮助信息并退出;
	--version 			显示版本信息并退出。
请注意,-d 和-t 选项可接受不同的时间/日期格式

2.命令示例

创建新文件,或在文件夹下创建新文件

[root@nie linux]# ll
总用量 12
drwxr-xr-x. 2 root root 20 6月   1 13:36 word
[root@nie linux]# touch touch.txt
[root@nie linux]# ll
总用量 12
-rw-r--r--. 1 root root  0 6月  16 12:37 touch.txt
drwxr-xr-x. 2 root root 20 6月   1 13:36 word
[root@nie linux]# ll aa/
总用量 4
-rw-r--r--. 1 777 root 22 6月   1 13:00 linux.txt
[root@nie linux]# touch aa/touch.txt
[root@nie linux]# ll aa/
总用量 4
-rw-r--r--. 1  777 root 22 6月   1 13:00 linux.txt
-rw-r--r--. 1 root root  0 6月  16 12:38 touch.txt

修改文件的访问(access)时间,修改文件的更改(modify)时间

[root@nie linux]# stat aa/linux.txt 
  文件:aa/linux.txt
  大小:22        	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:3400403     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(  777/ UNKNOWN)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2023-06-01 13:35:02.304561000 +0800
最近更改:2023-06-01 13:00:25.626698812 +0800
最近改动:2023-06-01 13:03:32.400686418 +0800
创建时间:-
[root@nie linux]# touch -a aa/linux.txt 
[root@nie linux]# touch -m aa/linux.txt 
[root@nie linux]# stat aa/linux.txt 
  文件:aa/linux.txt
  大小:22        	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:3400403     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(  777/ UNKNOWN)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2023-06-16 12:44:14.090956546 +0800
最近更改:2023-06-16 13:17:20.507824723 +0800
最近改动:2023-06-16 13:17:20.507824723 +0800
创建时间:-

指定时间更改文件的访问和更改时间

[root@nie linux]# stat aa/touch.txt 
  文件:aa/touch.txt
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:35421707    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2023-06-16 12:38:12.999980508 +0800
最近更改:2023-06-16 12:38:12.999980508 +0800
最近改动:2023-06-16 12:38:12.999980508 +0800
创建时间:-
[root@nie linux]# touch -at 202206161327.30 aa/touch.txt 
[root@nie linux]# touch -mt 202206161327.30 aa/touch.txt 
[root@nie linux]# stat aa/touch.txt 
  文件:aa/touch.txt
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:35421707    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2022-06-16 13:27:30.000000000 +0800
最近更改:2022-06-16 13:27:30.000000000 +0800
最近改动:2023-06-16 13:28:26.138780551 +0800
创建时间:-
[root@nie linux]# touch -t 202106161327.30 aa/touch.txt 
[root@nie linux]# stat aa/touch.txt 
  文件:aa/touch.txt
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:35421707    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2021-06-16 13:27:30.000000000 +0800
最近更改:2021-06-16 13:27:30.000000000 +0800
最近改动:2023-06-16 13:30:49.678771025 +0800
创建时间:-

[root@nie linux]# stat touch.txt 
  文件:touch.txt
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:3400402     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2023-06-16 12:37:39.765982714 +0800
最近更改:2023-06-16 12:37:39.765982714 +0800
最近改动:2023-06-16 12:37:39.765982714 +0800
创建时间:-
[root@nie linux]# touch -d 20010101 01:01:01 touch.txt 
[root@nie linux]# stat touch.txt 
  文件:touch.txt
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:3400402     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2001-01-01 00:00:00.000000000 +0800
最近更改:2001-01-01 00:00:00.000000000 +0800
最近改动:2023-06-16 16:19:48.872687727 +0800
创建时间:-

说明

stat命令用于显示文件的状态信息。
stat命令可以查看的信息包括:
文件(File):显示文件名
大小(Size):显示文件大小
块(Blocks):文件使用的数据块总数
IO块(IO Block):IO块大小
文件类型(regular file):文件类型(普通文件)
设备(Device):设备编号
Inode:Inode号
硬链接(Links):链接数
权限(Access):文件的权限
Gid、Uid:文件所有权的Gid和Uid
访问时间(access time):表示我们最后一次访问(仅仅是访问,没有改动)文件的时间
更改时间(modify time):表示我们最后一次修改文件的时间
改动时间(change time):表示我们最后一次对文件属性改变的时间,包括权限,大小,属性等等
创建时间(Birth time) : 文件创建时间,crtime,不过据查此属性linux已废弃,目前状态显示结果均为-

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐