[Linux Learning] Essential Linux Commands (1) – Detailed Explanation of the Touch Command
1. Detailed explanation of commands
The touch command is mainly used to create ordinary files. The usage is touch test.txt. If the file exists, it means modifying the current file time. The common parameters are detailed as follows:
用法: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. Command examples
Create a new file, or create a new file under a folder
[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
Modify the file's access (access) time, modify the file's change (modify) time
[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
创建时间:-
Specify time to change file access and change times
[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
创建时间:-
illustrate
The stat command is used to display file status information.
The information that can be viewed with the stat command includes:
File: displays the file name
Size: displays the file size
Blocks: the total number of data blocks used by
the file IO Block: IO block size
file type (regular file ): File type (ordinary file)
Device (Device): Device number
Inode: Inode number
Hard links (Links): Number of links
Permissions (Access): File permissions
Gid, Uid: Gid and Uid of file ownership
Access time (access time) ): Indicates the time when we last accessed (only accessed, not modified) the file.
Modify time: Indicates the time when we last modified the file.
Change time: Indicates the time when we last changed the file attributes. , including permissions, size, attributes, etc.
Creation time (Birth time): file creation time, crtime, but according to the investigation, this attribute has been abandoned in Linux, and the current status display results are -