[命令] Linux 命令 stat 的使用 (显示文件的状态)

内容一:stat 命令的使用格式

1.1 一般的使用格式

# stat <file>

1.2 使用选项的格式

# stat <option> <parameter> <file>

内容二:stat 命令的常用选项

1) -c 或者 –format=FORMAT,只显示文件的时间
2) –printf=FORMAT,只显示文件的时间同时不换行打印

内容三:stat 命令的常用参数

1) %a, access rights in octal (note ‘#’ and ‘0’ printf flags)
2) %A, access rights in human readable form
3) %b, number of blocks allocated (see %B)
4) %B, the size in bytes of each block reported by %b
5) %C, SELinux security context string
6) %d, device number in decimal
7) %D, device number in hex
8) %f, raw mode in hex
9) %F, file type
10) %g, group ID of owner
11) %G, group name of owner
12) %h, number of hard links
13) %i, inode number
14) %m, mount point
15) %n, file name
16) %N, quoted file name with dereference if symbolic link
17) %o, optimal I/O transfer size hint
18) %s, total size, in bytes
19) %t, major device type in hex, for character/block device special files
20) %T, minor device type in hex, for character/block device special files
21) %u, user ID of owner
22) %U, user name of owner
23) %w, time of file birth, human-readable; – if unknown
24) %W, time of file birth, seconds since Epoch; 0 if unknown
25) %x, time of last access, human-readable
26) %X, time of last access, seconds since Epoch
27) %y, time of last data modification, human-readable
28) %Y, time of last data modification, seconds since Epoch
29) %z, time of last status change, human-readable
30) %Z, time of last status change, seconds since Epoch

内容四:stat 命令的使用案例

4.1 案例一:显示某个文件的时间

[mingyuzhu@liux ~]$  stat -c %y test.txt 
2025-03-31 17:26:11.399060089 +0800

或者:

[mingyuzhu@liux ~]$  stat --format=%y test.txt 
2025-03-31 17:26:11.399060089 +0800

(补充:这里以显示 test.txt 文件的时间为例)

4.2 案例二:显示某个文件的时间 (不换行)

[mingyuzhu@liux ~]$  stat --printf=%y test.txt 
2025-03-31 17:26:11.399060089 +0800[mingyuzhu@liux ~]$ 

(补充:这里以显示 test.txt 文件的时间为例)

[命令] Linux 命令 declare (声明变量)

内容一:declare 命令的格式

# declare <option> <variable>

内容二:declare 命令的常用选项

1) -a,声明变量为数组
2) -A,声明变量为关联数组
3) -i,声明变量为整数
4) -l,将变量名转换为小写
5) -g,声明变量为全局变量
6) -r,让变量名只读
7) -u,将变量名转换为大写
8) -x,将变量输出到输出到子 Shell 中,其实就是将此变量设置成全局变量

[内容] Shell 文本掐头去尾的使用

内容一:掐头

1.1 只掐头从左数第 1 个出现的某个字符和此字符前面的所有内容

# complete=/dir1/dir2/dir3/dir4/test.file.txt
# echo ${complete#*/}
dir1/dir2/dir3/dir4/test.file.txt

(补充:这里以掐掉从左数第 1 个斜杠 “/” 以及前面的所有所有内容为例)

1.2 掐头从左数最后 1 个出现的某个字符和此字符前面的所有内容

# complete=/dir1/dir2/dir3/dir4/test.file.txt
# echo ${complete##*/}
test.file.txt

(补充:这里以掐掉从左数最后 1 个斜杠 “/” 以及前面的所有所有内容为例)

内容二:去尾

2.1 只去尾从右数第 1 个出现的某个字符和此字符后面的所有内容

# complete=/dir1/dir2/dir3/dir4/test.file.txt
# echo ${complete%.*}
/dir1/dir2/dir3/dir4/test.file

(补充:这里以掐掉从右数第 1 个点 “.” 以及后面的所有所有内容为例)

2.2 去尾从右数最后 1 个出现的某个字符和此字符后面的所有内容

# complete=/dir1/dir2/dir3/dir4/test.file.txt
# echo ${complete%%.*}
/dir1/dir2/dir3/dir4/test

(补充:这里以掐掉从右数最后 1 个点 “.” 以及后面的所有所有内容为例)

[命令] Linux 命令 basename (去除目录或文件的路径只显示此目录或文件)

案例一:去除某个目录或文件的路径只显示此目录或文件

# basename /root/test.txt
test.txt

(补充:这里以去除文件 /root/test.txt 的路径为例)

案例二:去除某 2 个目录或文件的路径只显示此 2 个目录或文件

2.1 去除某 2 个目录或文件的路径只显示此 2 个目录或文件 (最终结果只显示了第 1 个目录或文件)

# basename /root/test.txt /home/mingyuzhu/mingyuzhu.txt
test.txt

(补充:这里以去除文件 /root/test.txt 和文件 /home/mingyuzhu/mingyuzhu.txt 的路径为例)

2.2 去除某 2 个目录或文件的路径只显示此 2 个目录或文件

# basename -a /root/test.txt /home/mingyuzhu/mingyuzhu.txt
test.txt mingyuzhu.txt

(补充:这里以去除文件 /root/test.txt 和文件 /home/mingyuzhu/mingyuzhu.txt 的路径为例)

案例三:去除某个目录或文件的路径只显示此目录或文件,同时去除这个目录或文件的后缀

# basename -s .txt /root/test.txt
test

(补充:这里以去除文件 /root/test.txt 的路径同时去除后缀 .txt 为例)