du命令

du命令的英文全称是“Disk Usage”,即用于查看磁盘占用空间的意思。但是与df命令不同的是,du命令是对文件和目录磁盘使用的空间的查看,而不是某个分区。

du 命令 表示 磁盘使用率Disk Usage。这是一个标准的 Unix 程序,用于估计当前工作目录中的文件空间使用情况。
它使用递归方式总结磁盘使用情况,以获取目录及其子目录的大小。
如同我说的那样, 使用 ls 命令时,目录大小仅显示 4KB。参见下面的输出。

常用参数

du参数 描述
-a 显示目录中所有文件大小
-k 以KB为单位显示文件大小
-m 以MB为单位显示文件大小
-g 以GB为单位显示文件大小
-h 以易读方式显示文件大小
-s 仅显示总计

文档

1
man du

或者

1
du --help

示例

du -hs 目录:获取目录的总大小

使用以下 du 命令格式获取给定目录的总大小。

1
du -hs 目录

在该示例中,我们将得到 /root/blog/ 目录的总大小。

1
2
3
[root@localhost blog]# du -hs /root/blog/
93M /root/blog/
[root@localhost blog]#

或者:

1
2
3
[root@localhost blog]# du -h --max-depth=0 /root/blog/
93M /root/blog/
[root@localhost blog]#

参数说明

du命令参数 描述
-h 以易读的格式显示大小 (例如 1K 234M 2G)
-s 仅显示每个参数的总数
–max-depth=N 目录的打印深度

du -h:获取每个目录的大小

使用以下 du 命令格式获取每个目录(包括子目录)的总大小。

在该示例中,我们将获得每个 /home/daygeek/Documents 目录及其子目录的总大小。

1
du -h /home/daygeek/Documents/ | sort -rh | head -20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost blog]# pwd
/root/blog
[root@localhost blog]# du -h |sort -rh|head -20
93M .
66M ./node_modules
7.3M ./.git
7.0M ./.git/objects
5.5M ./.git/objects/pack
5.2M ./node_modules/moment
5.0M ./node_modules/hexo-generator-baidu-sitemap/node_modules
5.0M ./node_modules/hexo-generator-baidu-sitemap
4.9M ./node_modules/lodash
4.4M ./node_modules/core-js
3.4M ./source
3.3M ./source/_posts
3.2M ./node_modules/prismjs
3.0M ./node_modules/hexo-deployer-git/node_modules
3.0M ./node_modules/hexo-deployer-git
3.0M ./node_modules/hexo-abbrlink
2.9M ./node_modules/hexo-generator-sitemap/node_modules
2.9M ./node_modules/hexo-generator-sitemap
2.9M ./node_modules/hexo-abbrlink/node_modules
2.7M ./node_modules/hexo-generator-sitemap/node_modules/highlight.js
[root@localhost blog]#

du -hs 目录/*:获取每个目录的摘要

使用如下 du 命令格式仅获取每个目录的摘要。

1
du -hs 目录/* | sort -rh | head -10

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost blog]# pwd
/root/blog
[root@localhost blog]# du -hs /root/blog/* | sort -rh | head -10
66M /root/blog/node_modules
14M /root/blog/db.json
3.4M /root/blog/source
2.4M /root/blog/themes
168K /root/blog/package-lock.json
16K /root/blog/scaffolds
4.0K /root/blog/StartWriting.bat
4.0K /root/blog/package.json
4.0K /root/blog/KillBlog.sh
4.0K /root/blog/HexoSTest.bat
[root@localhost blog]#

du -hS:获取每个目录的不含子目录的大小

使用如下 du 命令格式来展示每个目录的总大小,不包括子目录。

1
du -hS 目录 | sort -rh | head -20

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost blog]# du -hS /root/blog/ |sort -rh |head -20
14M /root/blog/
5.5M /root/blog/.git/objects/pack
3.3M /root/blog/node_modules/lodash
2.4M /root/blog/node_modules/prismjs/components
2.2M /root/blog/node_modules/moment/min
1.7M /root/blog/node_modules/lodash/fp
1.7M /root/blog/node_modules/hexo-generator-sitemap/node_modules/highlight.js/lib/languages
1.7M /root/blog/node_modules/hexo-deployer-git/node_modules/highlight.js/lib/languages
1.7M /root/blog/node_modules/hexo-abbrlink/node_modules/highlight.js/lib/languages
1.5M /root/blog/node_modules/nunjucks/browser
1.5M /root/blog/node_modules/highlight.js/lib/languages
1.2M /root/blog/node_modules/hexo-generator-baidu-sitemap/node_modules/acorn/dist
1.2M /root/blog/node_modules/acorn/dist
916K /root/blog/node_modules/core-js/modules
900K /root/blog/node_modules/moment-timezone/builds
880K /root/blog/node_modules/core-js/library/modules
824K /root/blog/node_modules/core-js/client
740K /root/blog/node_modules/moment/locale
660K /root/blog/node_modules/css/node_modules/source-map/dist
652K /root/blog/node_modules/moment/src/locale
[root@localhost blog]#

du -h –max-depth=1:仅获取一级子目录的大小

如果要获取 Linux 上给定目录的一级子目录(包括其子目录)的大小,请使用以下命令格式。

1
2
3
4
5
6
7
8
[root@localhost blog]# du -h --max-depth=1 /root/blog/
7.3M /root/blog/.git
16K /root/blog/scaffolds
3.4M /root/blog/source
2.4M /root/blog/themes
66M /root/blog/node_modules
93M /root/blog/
[root@localhost blog]#

du -hsc:统计

如果要在 du 命令输出中获得总计,请使用以下 du 命令格式。

1
du -hsc 目录/* | sort -rh | head -10

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost blog]# du -hsc /root/blog/* | sort -rh
86M 总用量
66M /root/blog/node_modules
14M /root/blog/db.json
3.4M /root/blog/source
2.4M /root/blog/themes
168K /root/blog/package-lock.json
16K /root/blog/scaffolds
4.0K /root/blog/StartWriting.bat
4.0K /root/blog/package.json
4.0K /root/blog/KillBlog.sh
4.0K /root/blog/HexoSTest.bat
4.0K /root/blog/hexos.sh
4.0K /root/blog/HexoS.bat
4.0K /root/blog/HexoD.bat
4.0K /root/blog/FM.properties
4.0K /root/blog/_config.yml
[root@localhost blog]#

参考资料

https://linux.cn/article-11503-1.html
https://www.yiibai.com/linux/du.html
https://www.runoob.com/linux/linux-comm-du.html
https://www.linuxcool.com/du
可以替代du的一些命令
https://linux.cn/article-10239-1.html

df命令

Linux df(英文全拼:disk free) 命令用于显示目前在 Linux 系统上的文件系统磁盘使用情况统计。

日常普遍用该命令可以查看磁盘被占用了多少空间、还剩多少空间等信息。

语法

1
df [OPTION]... [FILE]...

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-a, --all
include pseudo, duplicate, inaccessible file systems

-B, --block-size=SIZE
scale sizes by SIZE before printing them; e.g., '-BM' prints sizes in units of 1,048,576 bytes; see SIZE format below

--direct
show statistics for a file instead of mount point

--total
produce a grand total

-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)

-H, --si
likewise, but use powers of 1000 not 1024

-i, --inodes
list inode information instead of block usage

-k like --block-size=1K

-l, --local
limit listing to local file systems

--no-sync
do not invoke sync before getting usage info (default)

--output[=FIELD_LIST]
use the output format defined by FIELD_LIST, or print all fields if FIELD_LIST is omitted.

-P, --portability
use the POSIX output format

--sync invoke sync before getting usage info

-t, --type=TYPE
limit listing to file systems of type TYPE

-T, --print-type
print file system type

-x, --exclude-type=TYPE
limit listing to file systems not of type TYPE

-v (ignored)

--help display this help and exit

--version
output version information and exit

实例

df

1
2
3
4
5
6
7
[root@localhost exam]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/block/bootdevice/by-name/userdata 56814352 11591764 45206204 21% /
tmpfs 1822552 364 1822188 1% /dev
tmpfs 1843032 0 1843032 0% /dev/shm
/data/media 56763152 11608148 45155004 21% /mnt/files
[root@localhost exam]#

输出结果分为六列。我们来看一下每一列的含义。

  • Filesystem – Linux 系统中的文件系统
  • 1K-blocks – 文件系统的大小,用 1K 大小的块来表示。
  • Used – 以 1K 大小的块所表示的已使用数量。
  • Available – 以 1K 大小的块所表示的可用空间的数量。
  • Use% – 文件系统中已使用的百分比。
  • Mounted on – 已挂载的文件系统的挂载点。

df -a

1
2
3
4
5
6
7
8
9
文件系统                                  1K-块     已用     可用 已用% 挂载点
/dev/block/bootdevice/by-name/userdata 56814352 11591764 45206204 21% /
proc 0 0 0 - /proc
sys 0 0 0 - /sys
tmpfs 1822552 364 1822188 1% /dev
tmpfs 1843032 0 1843032 0% /dev/shm
devpts 0 0 0 - /dev/pts
/data/media 56763152 11608148 45155004 21% /mnt/files
[root@localhost exam]#

df -h

在上面的示例中你可能已经注意到了,它使用 1K 大小的块为单位来表示使用情况,如果你以人类友好格式来显示它们,可以使用 -h 标志。

1
2
3
4
5
6
7
[root@localhost exam]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/block/bootdevice/by-name/userdata 55G 12G 44G 21% /
tmpfs 1.8G 364K 1.8G 1% /dev
tmpfs 1.8G 0 1.8G 0% /dev/shm
/data/media 55G 12G 44G 21% /mnt/files
[root@localhost exam]#

可用现在的容量,已用,可用,这三列则都是以 GB,MB,KB等为单位来显示的。

df -H

1
2
3
4
5
6
7
[root@localhost exam]# df -H
文件系统 容量 已用 可用 已用% 挂载点
/dev/block/bootdevice/by-name/userdata 59G 12G 47G 21% /
tmpfs 1.9G 373k 1.9G 1% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
/data/media 59G 12G 47G 21% /mnt/files
[root@localhost exam]#

df -i 以inode模式来显示磁盘使用情况

通过使用 -i 标记来列出节点而不是块的使用情况。

1
2
3
4
5
6
7
[root@localhost exam]# df -i
文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/block/bootdevice/by-name/userdata 3620864 121008 3499856 4% /
tmpfs 455638 658 454980 1% /dev
tmpfs 460758 1 460757 1% /dev/shm
/data/media 3620864 121008 3499856 4% /mnt/files
[root@localhost exam]#

df -k

1
2
3
4
5
6
7
[root@localhost exam]# df -k
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/block/bootdevice/by-name/userdata 56814352 11591724 45206244 21% /
tmpfs 1822552 364 1822188 1% /dev
tmpfs 1843032 0 1843032 0% /dev/shm
/data/media 56763152 11608108 45155044 21% /mnt/files
[root@localhost exam]#

df -l

1
2
3
4
5
6
[root@localhost exam]# df -l
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/block/bootdevice/by-name/userdata 56814352 11591948 45206020 21% /
tmpfs 1822552 364 1822188 1% /dev
tmpfs 1843032 0 1843032 0% /dev/shm
/data/media 56763152 11608332 45154820 21% /mnt/files

df -m:以MB为单位显示文件系统的磁盘使用情况

以 MB 为单位来显示文件系统磁盘空间使用情况,使用 -m 标志。

1
2
3
4
5
6
7
[root@localhost exam]# df -m
文件系统 1M-块 已用 可用 已用% 挂载点
/dev/block/bootdevice/by-name/userdata 55483 11321 44147 21% /
tmpfs 1780 1 1780 1% /dev
tmpfs 1800 0 1800 0% /dev/shm
/data/media 55433 11337 44097 21% /mnt/files
[root@localhost exam]#

df -T

使用 -T 标志显示文件系统类型。

1
2
3
4
5
6
[root@localhost exam]# df -T
文件系统 类型 1K-块 已用 可用 已用% 挂载点
/dev/block/bootdevice/by-name/userdata ext4 56814352 11592360 45205608 21% /
tmpfs tmpfs 1822552 364 1822188 1% /dev
tmpfs tmpfs 1843032 0 1843032 0% /dev/shm
/data/media sdcardfs 56763152 11608744 45154408 21% /mnt/files

df -t 文件系统类型:显示指定类型的文件系统的磁盘使用情况

我们可以限制仅列出某些文件系统。比如,只列出 ext4 文件系统。我们使用 -t 标志。

1
2
3
4
[root@localhost exam]# df -t ext4
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/block/bootdevice/by-name/userdata 56814352 11592032 45205936 21% /
[root@localhost exam]#

df -x 文件系统类型:

有时,我们可能需要从结果中去排除指定类型的文件系统。我们可以使用 -x 标记达到我们的目的。

1
2
3
4
5
6
[root@localhost exam]# df -x ext4
文件系统 1K-块 已用 可用 已用% 挂载点
tmpfs 1822552 364 1822188 1% /dev
tmpfs 1843032 0 1843032 0% /dev/shm
/data/media 56763152 11608420 45154732 21% /mnt/files
[root@localhost exam]#

显示一个目录的磁盘使用情况

去显示某个目录的硬盘空间使用情况以及它的挂载点,例如~/share/目录,可以使用如下的命令:

1
2
3
4
[root@localhost exam]#  df -hT ~/share/
文件系统 类型 容量 已用 可用 已用% 挂载点
/data/media sdcardfs 55G 12G 44G 21% /mnt/files
[root@localhost exam]#

参考资料

https://linux.cn/article-10096-1.html
https://www.runoob.com/linux/linux-comm-df.html
https://www.yiibai.com/linux/df.html
https://www.linuxcool.com/df

ls命令含义

Linux ls(英文全拼:list files)命令用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录)。

语法

1
ls [-alrtAFR] [name...]

参数

参数 描述
-a 显示所有文件及目录 (. 开头的隐藏文件也会列出)
-A 同 -a ,但不列出 “.” (目前目录) 及 “..” (父目录)
-F 在列出的文件名称后加一符号;例如可执行档则加 “*”, 目录则加 “/“
-l 除文件名称外,亦将文件型态、权限、拥有者、文件大小等资讯详细列出
-r 将文件以相反次序显示(原定依英文字母次序)
-t 将文件依建立时间之先后次序列出(时间越近越在前面)
-R 若目录下有文件,则以下之文件亦皆依序列出

man ls

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
LS(1)                                                                 General Commands Manual                                                                LS(1)

NAME
ls, dir, vdir - 列目录内容

提要
ls [选项] [文件名...]

POSIX 标准选项: [-CFRacdilqrtu1]

GNU 选项 (短格式):
[-1abcdfgiklmnopqrstuxABCDFGLNQRSUX] [-w cols] [-T cols] [-I pattern] [--full-time] [--format={long,verbose,commas,across,vertical,single-column}]
[--sort={none,time,size,extension}] [--time={atime,access,use,ctime,status}] [--color[={none,auto,always}]] [--help] [--version] [--]

描述( DESCRIPTION )
程序ls先列出非目录的文件项,然后是每一个目录中的“可显示”文件。如果 没有选项之外的参数【译注:即文件名部分为空】出现,缺省为 "." (当前目录)。 选项“ -d
”使得目录与非目录项同样对待。除非“ -a ” 选项出现,文 件名以“.”开始的文件不属“可显示”文件。

以当前目录为准,每一组文件(包括非目录文件项,以及每一内含文件的目录)分 别按文件名比较顺序排序。如果“ -l ”选项存在,每组文件前显示一摘要行:
给出该组文件长度之和(以 512 字节为单位)。

输出是到标准输出( stdout )。除非以“ -C ”选项要求按多列输出,输出 将是一行一个。然而,输出到终端时,单列输出或多列输出是不确定的。可以分别 用选项“ -1 ”
或“ -C ”来强制按单列或多列输出。

-C 多列输出,纵向排序。

-F 每个目录名加“ / ”后缀,每个 FIFO 名加“ | ”后缀, 每个可运行名加“ * ”后缀。

-R 递归列出遇到的子目录。

-a 列出所有文件,包括以 "." 开头的隐含文件。

-c 使用“状态改变时间”代替“文件修改时间”为依据来排序 (使用“ -t ”选项时)或列出(使用“ -l ”选项时)。

-d 将目录名象其它文件一样列出,而不是列出它们的内容。

-i 输出文件前先输出文件系列号(即 i 节点号: i-node number)。 -l 列出(以单列格式)文件模式( file mode ),文件的链
接数,所有者名,组名,文件大小(以字节为单位),时间信 息,及文件名。缺省时,时间信息显示最近修改时间;可以以 选项“ -c ”和“ -u
”选择显示其它两种时间信息。对于设 备文件,原先显示文件大小的区域通常显示的是主要和次要的 号(majorand minor device numbers)。

-q 将文件名中的非打印字符输出为问号。(对于到终端的输出这是缺省的。)

-r 逆序排列。

-t 按时间信息排序。

-u 使用最近访问时间代替最近修改时间为依据来排序(使用 “ -t ”选项时)或列出(使用“ -l ”选项时)。

-1 单列输出。

GNU 细节
如果标准输出是终端,将多列输出(纵向排序)。

dir ( 也被安装为命令 d ) 等同于“ ls -C ”;即,文件
缺省是多列输出,纵向排序。vdir ( 也被安装为命令 v ) 等同于“ ls -l ”; 即,文件缺省是按长格式输出。

GNU 选项
-1, --format=single-column 一行输出一个文件(单列输出)。如标准输出不是到终端, 此选项就是缺省选项。

-a, --all
列出目录中所有文件,包括以“.”开头的文件。

-b, --escape
把文件名中不可输出的字符用反斜杠加字符编号(就象在 C 语言里一样)的形式列出。

-c, --time=ctime, --time=status
按文件状态改变时间(i节点中的ctime)排序并输出目录内 容。如采用长格式输出(选项“-l”),使用文件的状态改
变时间取代文件修改时间。【译注:所谓文件状态改变(i节 点中以ctime标志),既包括文件被修改,又包括文件属性( 如所有者、组、链接数等等)的变化】

-d, --directory
将目录名象其它文件一样列出,而不是列出它们的内容。

-f 不排序目录内容;按它们在磁盘上存储的顺序列出。同时启 动“ -a ”选项,如果在“ -f ”之前存在“ -l ”、“ - -color ”或“ -s ”,则禁止它们。

-g 忽略,为兼容UNIX用。

-i, --inode
在每个文件左边打印 i 节点号(也叫文件序列号和索引号: file serial number and index number)。i节点号在每个特定的文件系统中是唯一的。

-k, --kilobytes
如列出文件大小,则以千字节KB为单位。

-l, --format=long, --format=verbose
除每个文件名外,增加显示文件类型、权限、硬链接数、所 有者名、组名、大小( byte )、及时间信息(如未指明是
其它时间即指修改时间)。对于6个月以上的文件或超出未来 1 小时的文件,时间信息中的时分将被年代取代。

每个目录列出前,有一行“总块数”显示目录下全部文件所 占的磁盘空间。块默认是 1024 字节;如果设置了 POSIXLY_CORRECT 的环境变量,除非用“ -k
”选项,则默认块大小是 512 字 节。每一个硬链接都计入总块数(因此可能重复计数),这无 疑是个缺点。

列出的权限类似于以符号表示(文件)模式的规范。但是 ls
在每套权限的第三个字符中结合了多位( multiple bits ) 的信息,如下: s 如果设置了 setuid 位或 setgid 位,而且也设置了相应的可执行位。 S 如果设置了
setuid 位或 setgid 位,但是没有设置相应的可执行位。 t 如果设置了 sticky 位,而且也设置了相应的可执行位。 T 如果设置了 sticky
位,但是没有设置相应的可执行位。 x 如果仅仅设置了可执行位而非以上四种情况。 - 其它情况(即可执行位未设置)。

-m, --format=commas
水平列出文件,每行尽可能多,相互用逗号和一个空格分隔。

-n, --numeric-uid-gid
列出数字化的 UID 和 GID 而不是用户名和组名。

-o 以长格式列出目录内容,但是不显示组信息。等于使用“ --format=long --no-group ”选项。提供此选项是为了与其它版本的 ls 兼容。

-p 在每个文件名后附上一个字符以说明该文件的类型。类似“ -F ”选项但是不 标示可执行文件。

-q, --hide-control-chars
用问号代替文件名中非打印的字符。这是缺省选项。

-r, --reverse
逆序排列目录内容。

-s, --size
在每个文件名左侧输出该文件的大小,以 1024 字节的块为单位。如果设置了 POSIXLY_CORRECT 的环境变量,除非用“ -k ”选项,块大小是 512 字节。

-t, --sort=time
按文件最近修改时间( i 节点中的 mtime )而不是按文件名字典序排序,新文件 靠前。

-u, --time=atime, --time=access, --time=use
类似选项“ -t ”,但是用文件最近访问时间( i 节点中的 atime )取代文件修 改时间。如果使用长格式列出,打印的时间是最近访问时间。

-w, --width cols
假定屏幕宽度是 cols ( cols 以实际数字取代)列。如未用此选项,缺省值是这 样获得的:如可能先尝试取自终端驱动,否则尝试取自环境变量 COLUMNS (如果设
置了的话),都不行则取 80 。

-x, --format=across, --format=horizontal
多列输出,横向排序。

-A, --almost-all
显示除 "." 和 ".." 外的所有文件。

-B, --ignore-backups
不输出以“ ~ ”结尾的备份文件,除非已经在命令行中给出。

-C, --format=vertical
多列输出,纵向排序。当标准输出是终端时这是缺省项。使用命令名 dir 和 d 时, 则总是缺省的。

-D, --dired
当采用长格式(“ -l ”选项)输出时,在主要输出后,额外打印一行: //DIRED// BEG1 END1 BEG2 END2 ...

BEGn 和 ENDn 是无符号整数,记录每个文件名的起始、结束位置在输出中的位置(
字节偏移量)。这使得 Emacs 易于找到文件名,即使文件名包含空格或换行等非正 常字符也无需特异的搜索。

如果目录是递归列出的(“ -R ”选项),每个子目录后列出类似一行:
//SUBDIRED// BEG1 END1 ... 【译注:我测试了 TurboLinux4.0 和 RedHat6.1 ,发现它们都是在 “ //DIRED// BEG1... ”之后列出“ //SUBDIRED// BEG1 ...
”,也即只有一个 而不是在每个子目录后都有。而且“ //SUBDIRED// BEG1 ... ”列出的是各个子目 录名的偏移。】

-F, --classify, --file-type
在每个文件名后附上一个字符以说明该文件的类型。“ * ”表示普通的可执行文件; “ / ”表示目录;“ @ ”表示符号链接;“ | ”表示FIFOs;“ = ”表示套接字 (sock‐
ets) ;什么也没有则表示普通文件。

-G, --no-group
以长格式列目录时不显示组信息。

-I, --ignorepattern
除非在命令行中给定,不要列出匹配 shell 文件名匹配式( pattern ,不是指一般 表达式)的文件。在 shell 中,文件名以 "." 起始的不与在文件名匹配式 (pat‐
tern) 开头的通配符匹配。

-L, --dereference
列出符号链接指向的文件的信息,而不是符号链接本身。

-N, --literal
不要用引号引起文件名。

-Q, --quote-name
用双引号引起文件名,非打印字符以 C 语言的方法表示。

-R, --recursive
递归列出全部目录的内容。

-S, --sort=size
按文件大小而不是字典序排序目录内容,大文件靠前。

-T, --tabsize cols
假定每个制表符宽度是 cols 。缺省为 8。为求效率, ls 可能在输出中使用制表符。 若 cols 为 0,则不使用制表符。

-U, --sort=none
不排序目录内容;按它们在磁盘上存储的顺序列出。(选项“ -U ”和“ -f ”的不 同是前者不启动或禁止相关的选项。)这在列很大的目录时特别有用,因为不加排序
能显著的加快速度。

-X, --sort=extension
按文件扩展名(由最后的 "." 之后的字符组成)的字典序排序。没有扩展名的先列 出。

--color[=when]
指定是否使用颜色区别文件类别。环境变量 LS_COLORS 指定使用的颜色。如何设置 这个变量见 dircolors(1) 。 when 可以被省略,或是以下几项之一:

none 不使用颜色,这是缺省项。
auto 仅当标准输出是终端时使用。 always 总是使用颜色。指定 --color 而且省略 when 时就等同于 --color=always 。

--full-time
列出完整的时间,而不是使用标准的缩写。格式如同 date(1) 的缺省格式;此格式 是不能改变的,但是你可以用 cut(1) 取出其中的日期字串并将结果送至命令 “
date -d ”。

输出的时间包括秒是非常有用的。( Unix 文件系统储存文件的时间信息精确到秒,
因此这个选项已经给出了系统所知的全部信息。)例如,当你有一个 Makefile 文件 不能恰当的生成文件时,这个选项会提供帮助。

GNU 标准选项
--help 打印用法信息到标准输出并顺利退出。

--version
打印版本信息到标准输出并顺利退出。

-- 结束选项表。

环境
变量 POSIXLY_CORRECT 可以决定一组选择。如果没有设置此变量,每个制表符的字 符数由变量 TABSIZE 决定。变量 COLUMNS (当它由一个十进制整数表示时)决定输
出的列宽度(同“ -C ”选项一起用时)。文件名不得为适应多列输出而被截断。变 量 LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES 及 LC_TIME 仍保持原义。 变量 TZ
给出时区供 ls 输出相应的时间字串。变量 LS_COLORS 用以决定是否使用 颜色。

已知错误
在 BSD 系统上,对于从 HP-UX 系统上通过 NFS mount 而来的文件,“ -s ”选项报 告的大小只有正确值的一半;在 HP-UX 系统上,对于从 BSD 系统上通过 NFS mount
而来的文件, ls 报告的大小则有正确值的两倍。这是 HP-UX 的一个缺陷造成的,它 也影响 HP-UX 上的 ls 程序。

适合到
POSIX 1003.2

参见
dircolors(1)

注意
本页描述的是 fileutils-3.16 文件包中的 ls ,其它版本的可能略有不同。纠错或添 加(功能)请 mailto: aeb@cwi.nl 和 aw@mail1.bet1.puv.fi 及 ragnar@light‐
side.ddns.org 。本程序的错误报告请 mailto: fileutils-bugs@gnu.ai.mit.edu 。

man ls英文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
LS(1)                                                                      User Commands                                                                     LS(1)

NAME
ls - list directory contents

SYNOPSIS
ls [OPTION]... [FILE]...

DESCRIPTION
List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.

-a, --all
do not ignore entries starting with .

-A, --almost-all
do not list implied . and ..

--author
with -l, print the author of each file

-b, --escape
print C-style escapes for nongraphic characters

--block-size=SIZE
scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE format below

-B, --ignore-backups
do not list implied entries ending with ~

-c with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort by name; otherwise: sort by
ctime, newest first

-C list entries by columns

--color[=WHEN]
colorize the output; WHEN can be 'never', 'auto', or 'always' (the default); more info below

-d, --directory
list directories themselves, not their contents

-D, --dired
generate output designed for Emacs' dired mode

-f do not sort, enable -aU, disable -ls --color

-F, --classify
append indicator (one of */=>@|) to entries

--file-type
likewise, except do not append '*'

--format=WORD
across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C

--full-time
like -l --time-style=full-iso

-g like -l, but do not list owner

--group-directories-first
group directories before files;

can be augmented with a --sort option, but any use of --sort=none (-U) disables grouping

-G, --no-group
in a long listing, don't print group names

-h, --human-readable
with -l, print sizes in human readable format (e.g., 1K 234M 2G)

--si likewise, but use powers of 1000 not 1024

-H, --dereference-command-line
follow symbolic links listed on the command line

--dereference-command-line-symlink-to-dir
follow each command line symbolic link

that points to a directory

--hide=PATTERN
do not list implied entries matching shell PATTERN (overridden by -a or -A)

--indicator-style=WORD
append indicator with style WORD to entry names: none (default), slash (-p), file-type (--file-type), classify (-F)

-i, --inode
print the index number of each file

-I, --ignore=PATTERN
do not list implied entries matching shell PATTERN

-k, --kibibytes
default to 1024-byte blocks for disk usage

-l use a long listing format

-L, --dereference
when showing file information for a symbolic link, show information for the file the link references rather than for the link itself

-m fill width with a comma separated list of entries

-n, --numeric-uid-gid
like -l, but list numeric user and group IDs

-N, --literal
print raw entry names (don't treat e.g. control characters specially)

-o like -l, but do not list group information

-p, --indicator-style=slash
append / indicator to directories

-q, --hide-control-chars
print ? instead of nongraphic characters

--show-control-chars
show nongraphic characters as-is (the default, unless program is 'ls' and output is a terminal)

-Q, --quote-name
enclose entry names in double quotes

--quoting-style=WORD
use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape

-r, --reverse
reverse order while sorting

-R, --recursive
list subdirectories recursively

-s, --size
print the allocated size of each file, in blocks

-S sort by file size

--sort=WORD
sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)

--time=WORD
with -l, show time as WORD instead of default modification time: atime or access or use (-u) ctime or status (-c); also use specified time as sort
key if --sort=time

--time-style=STYLE
with -l, show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT; FORMAT is interpreted like in 'date'; if FORMAT is FORMAT1<new‐
line>FORMAT2, then FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is prefixed with 'posix-', STYLE takes effect only out‐
side the POSIX locale

-t sort by modification time, newest first

-T, --tabsize=COLS
assume tab stops at each COLS instead of 8

-u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time

-U do not sort; list entries in directory order

-v natural sort of (version) numbers within text

-w, --width=COLS
assume screen width instead of current value

-x list entries by lines instead of by columns

-X sort alphabetically by entry extension

-1 list one file per line

SELinux options:

--lcontext
Display security context. Enable -l. Lines will probably be too wide for most displays.

-Z, --context
Display security context so it fits on most displays. Displays only mode, user, group, security context and file name.

--scontext
Display only security context and file name.

--help display this help and exit

--version
output version information and exit

SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

Using color to distinguish file types is disabled both by default and with --color=never. With --color=auto, ls emits color codes only when standard out‐
put is connected to a terminal. The LS_COLORS environment variable can change the settings. Use the dircolors command to set it.

Exit status:
0 if OK,

1 if minor problems (e.g., cannot access subdirectory),

2 if serious trouble (e.g., cannot access command-line argument).

GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report ls translation bugs to <http://translationproject.org/team/>

AUTHOR
Written by Richard M. Stallman and David MacKenzie.

COPYRIGHT
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
The full documentation for ls is maintained as a Texinfo manual. If the info and ls programs are properly installed at your site, the command

info coreutils 'ls invocation'

should give you access to the complete manual.

实例

列出根目录()下的所有目录:

1
2
[root@localhost exam]# ls /
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

ls -a

1
2
[root@localhost exam]# ls -a /
. .. bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

ls -A

1
2
[root@localhost exam]# ls -A /
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

ls -F

1
2
3
4
5
6
7
[root@localhost exam]# ls
_config.yml FM.properties HexoS.bat HexoSTest.bat package.json scaffolds StartWriting.bat
db.json HexoD.bat hexos.sh node_modules package-lock.json source themes
[root@localhost exam]# ls -F
_config.yml FM.properties HexoS.bat HexoSTest.bat package.json scaffolds/ StartWriting.bat
db.json HexoD.bat hexos.sh* node_modules/ package-lock.json source/ themes/
[root@localhost exam]#

ls -l

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost exam]# ls -l
总用量 3640
-rw-r--r--. 1 root root 3093 3月 19 21:12 _config.yml
-rw-r--r--. 1 root root 3498022 3月 22 20:29 db.json
-rw-r--r--. 1 root root 158 3月 19 21:12 FM.properties
-rw-r--r--. 1 root root 576 3月 19 21:12 HexoD.bat
-rw-r--r--. 1 root root 431 3月 22 20:52 HexoS.bat
-rwxr--r--. 1 root root 174 3月 19 21:16 hexos.sh
-rw-r--r--. 1 root root 376 3月 19 21:12 HexoSTest.bat
drwxr-xr-x. 308 root root 8192 3月 19 21:19 node_modules
-rw-r--r--. 1 root root 817 3月 19 21:12 package.json
-rw-r--r--. 1 root root 170424 3月 19 21:19 package-lock.json
drwxr-xr-x. 2 root root 4096 3月 19 21:12 scaffolds
drwxr-xr-x. 9 root root 4096 3月 19 21:12 source
-rw-r--r--. 1 root root 422 3月 19 21:12 StartWriting.bat
drwxr-xr-x. 3 root root 4096 3月 19 21:12 themes
[root@localhost exam]#

ls -l各列含义

列数 描述
第一列 共10位,第1位表示文档类型,d表示目录,-表示文件,l表示链接文件,d表示可随机存取的设备,如U盘等,c表示一次性读取设备,如鼠标、键盘等。后9位,依次对应三种身份所拥有的权限,身份顺序为:owner、group、others,权限顺序为:readable、writable、excutable。如:-r-xr-x—的含义为当前文档是一个文件,拥有者可读、可执行,同一个群组下的用户,可读、可写,其他人没有任何权限。
第二列 表示链接数,表示有多少个文件链接到inode号码。
第三列 表示拥有者
第四列 表示所属群组
第五列 表示文档容量大小,单位字节
第六列 表示文档最后修改时间,注意不是文档的创建时间哦
第七列 表示文档名称。以点(.)开头的是隐藏文档

ls -lh

参考资料:https://www.cnblogs.com/sparkdev/p/7476005.html
在 Linux 命令中,涉及到文件大小的地方,一般默认是以字节为单位显示的。这样可读性就不是很好。所以有了 -h 选项!这个选项的全称是--human-readable(给人读的)。也就是以 K, M, G 等单位来显示文件的大小:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost exam]# ls -lh
总用量 3.6M
-rw-r--r--. 1 root root 3.1K 3月 19 21:12 _config.yml
-rw-r--r--. 1 root root 3.4M 3月 22 20:29 db.json
-rw-r--r--. 1 root root 158 3月 19 21:12 FM.properties
-rw-r--r--. 1 root root 576 3月 19 21:12 HexoD.bat
-rw-r--r--. 1 root root 431 3月 22 20:52 HexoS.bat
-rwxr--r--. 1 root root 174 3月 19 21:16 hexos.sh
-rw-r--r--. 1 root root 376 3月 19 21:12 HexoSTest.bat
drwxr-xr-x. 308 root root 8.0K 3月 19 21:19 node_modules
-rw-r--r--. 1 root root 817 3月 19 21:12 package.json
-rw-r--r--. 1 root root 167K 3月 19 21:19 package-lock.json
drwxr-xr-x. 2 root root 4.0K 3月 19 21:12 scaffolds
drwxr-xr-x. 9 root root 4.0K 3月 19 21:12 source
-rw-r--r--. 1 root root 422 3月 19 21:12 StartWriting.bat
drwxr-xr-x. 3 root root 4.0K 3月 19 21:12 themes
[root@localhost exam]#

ls -l –full-time显示完整时间 显示年月日

ls -t 按时间排序列出 时间越近越在前

1
2
3
4
[root@localhost exam]# ls -t
HexoS.bat package-lock.json hexos.sh themes FM.properties HexoSTest.bat scaffolds
db.json node_modules source _config.yml HexoD.bat package.json StartWriting.bat
[root@localhost exam]#

ls -tr 按时间排序 时间越远越在前

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost 专项练习]# ls -lt --full-time
总用量 36
drwxr-xr-x. 5 root root 4096 2021-04-12 23:05:19.448995210 +0800 Linux
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.027846496 +0800 操作系统
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.027846496 +0800 计算机网络
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.027846496 +0800 数据库
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.027846496 +0800 智力题
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.017846496 +0800 JavaScript
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.017846496 +0800 加密和安全
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:00.987846496 +0800 HTML CSS
drwxr-xr-x. 5 root root 4096 2021-03-19 21:12:12.596962591 +0800 Java
[root@localhost 专项练习]# ls -ltr --full-time
总用量 36
drwxr-xr-x. 5 root root 4096 2021-03-19 21:12:12.596962591 +0800 Java
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:00.987846496 +0800 HTML CSS
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.017846496 +0800 加密和安全
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.017846496 +0800 JavaScript
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.027846496 +0800 智力题
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.027846496 +0800 数据库
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.027846496 +0800 计算机网络
drwxr-xr-x. 2 root root 4096 2021-04-03 18:10:01.027846496 +0800 操作系统
drwxr-xr-x. 5 root root 4096 2021-04-12 23:05:19.448995210 +0800 Linux
[root@localhost 专项练习]#

ls -R

ls Java:

1
2
3
4
5
6
[root@localhost 专项练习]# pwd
/root/exam/source/_posts/牛客网/专项练习
[root@localhost 专项练习]# ls
HTML CSS Java JavaScript Linux 操作系统 计算机网络 加密和安全 数据库 智力题
[root@localhost 专项练习]# ls Java
2019年10月 2019年11月 2019年12月

ls -R Java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost 专项练习]# ls -R Java
Java:
2019年10月 2019年11月 2019年12月

Java/2019年10月:
2019年10月28日 java 1.md 2019年10月29日 Java 1.md 2019年10月29日 java 3.md 2019年10月29日 java 5.md 2019年10月30日 java 2.md 2019年10月30日 java 4.md
2019年10月28日 java 2.md 2019年10月29日 java 2.md 2019年10月29日 java 4.md 2019年10月30日 java 1.md 2019年10月30日 java 3.md 2019年10月30日 java 5.md

Java/2019年11月:
2019年11月10日 Java1.md 2019年11月14日 java2.md 2019年11月22日 java1.md 2019年11月28日 java2.md 2019年11月5日 Java 2.md 2019年11月8日 Java1.md
2019年11月11日 java1.md 2019年11月15日 java1.md 2019年11月23日 java1.md 2019年11月29日 java1.md 2019年11月5日 java3.md 2019年11月8日 java2.md
2019年11月11日 Java2.md 2019年11月15日 java2.md 2019年11月23日 java2.md 2019年11月2日 java 1.md 2019年11月6日 java1.md 2019年11月9日 java1.md
2019年11月13日 java1.md 2019年11月15日 java3.md 2019年11月24日 java1.md 2019年11月4日 java 1.md 2019年11月6日 java2.md 2019年11月9日 java2.md
2019年11月14日 java1.md 2019年11月16日 java1.md 2019年11月28日 java1.md 2019年11月5日 Java 1.md 2019年11月7日 java3.md

Java/2019年12月:
2019年12月10日 java1.md 2019年12月11日 java2.md 2019年12月13日 java2.md 2019年12月17日 java1.md 2019年12月25日 java1.md
2019年12月10日 java2.md 2019年12月12日 java1.md 2019年12月14日 java1.md 2019年12月23日 java1.md 2019年12月30日 java1.md
2019年12月10日 java3.md 2019年12月13日 java1.md 2019年12月16日 java1.md 2019年12月24日 java1.md 2019年12月8日 java2.md
[root@localhost 专项练习]#

ls -s:列出文件的大小

1
2
3
4
5
6
[root@localhost 2021年04月]# ls -s
总用量 148
16 2021年04月09日Linux专项练习1.md 16 2021年04月12日Linux专项练习2.md 12 2021年04月18日Linux专项练习1.md 12 2021年04月18日Linux专项练习4.md
16 2021年04月11日Linux专项练习1.md 16 2021年04月12日Linux专项练习3.md 12 2021年04月18日Linux专项练习2.md 12 2021年04月18日Linux专项练习5.md
12 2021年04月12日Linux专项练习1.md 12 2021年04月17日Linux专项联系1.md 12 2021年04月18日Linux专项练习3.md
[root@localhost 2021年04月]#

ls -hs:列出文件大小,阅读友好显示

1
2
3
4
5
6
[root@localhost 2021年04月]# ls -hs
总用量 148K
16K 2021年04月09日Linux专项练习1.md 16K 2021年04月12日Linux专项练习2.md 12K 2021年04月18日Linux专项练习1.md 12K 2021年04月18日Linux专项练习4.md
16K 2021年04月11日Linux专项练习1.md 16K 2021年04月12日Linux专项练习3.md 12K 2021年04月18日Linux专项练习2.md 12K 2021年04月18日Linux专项练习5.md
12K 2021年04月12日Linux专项练习1.md 12K 2021年04月17日Linux专项联系1.md 12K 2021年04月18日Linux专项练习3.md
[root@localhost 2021年04月]#

ls -S:按文件大小而不是字典序排序目录内容,大文件靠前。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost 2021年04月]# ls -s|sort -r|head
总用量 148
16 2021年04月12日Linux专项练习3.md
16 2021年04月12日Linux专项练习2.md
16 2021年04月11日Linux专项练习1.md
16 2021年04月09日Linux专项练习1.md
12 2021年04月18日Linux专项练习5.md
12 2021年04月18日Linux专项练习4.md
12 2021年04月18日Linux专项练习3.md
12 2021年04月18日Linux专项练习2.md
12 2021年04月18日Linux专项练习1.md
[root@localhost 2021年04月]# ls -S|head
2021年04月11日Linux专项练习1.md
2021年04月09日Linux专项练习1.md
2021年04月12日Linux专项练习2.md
2021年04月12日Linux专项练习3.md
2021年04月18日Linux专项练习5.md
2021年04月12日Linux专项练习1.md
2021年04月17日Linux专项联系1.md
2021年04月18日Linux专项练习1.md
2021年04月18日Linux专项练习2.md
2021年04月18日Linux专项练习3.md
[root@localhost 2021年04月]#

ls与其他命令组合使用

查看当前目录下大小最大的前10个文件:

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost 2021年04月]# ls -sh|sort -r|head
总用量 148K
16K 2021年04月12日Linux专项练习3.md
16K 2021年04月12日Linux专项练习2.md
16K 2021年04月11日Linux专项练习1.md
16K 2021年04月09日Linux专项练习1.md
12K 2021年04月18日Linux专项练习5.md
12K 2021年04月18日Linux专项练习4.md
12K 2021年04月18日Linux专项练习3.md
12K 2021年04月18日Linux专项练习2.md
12K 2021年04月18日Linux专项练习1.md
[root@localhost 2021年04月]#

查看当前目录下大小最小的前10个文件:

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost 2021年04月]# ls -sh|sort|head
12K 2021年04月12日Linux专项练习1.md
12K 2021年04月17日Linux专项联系1.md
12K 2021年04月18日Linux专项练习1.md
12K 2021年04月18日Linux专项练习2.md
12K 2021年04月18日Linux专项练习3.md
12K 2021年04月18日Linux专项练习4.md
12K 2021年04月18日Linux专项练习5.md
16K 2021年04月09日Linux专项练习1.md
16K 2021年04月11日Linux专项练习1.md
16K 2021年04月12日Linux专项练习2.md
[root@localhost 2021年04月]#

参考资料

https://www.runoob.com/linux/linux-comm-ls.html

解除牛客网复制限制

我想把牛客网上面的错题复制下来,保存到自己的博客中,但是牛客网的网页,禁止了复制功能。所以有了这篇文章

安装用户脚本管理器

要使用用户脚本,您首先需要安装一个用户脚本管理器。您可以根据使用的浏览器来选择用户脚本管理器:
具体内容见:https://greasyfork.org/zh-CN上的介绍

下载脚本

进入下载Greasy Fork的地址:https://greasyfork.org/zh-CN,在搜索栏上输入:网页限制解除搜索脚本。

使用油猴脚本

安装好 网页限制解除:https://greasyfork.org/zh-CN/scripts/14146-%E7%BD%91%E9%A1%B5%E9%99%90%E5%88%B6%E8%A7%A3%E9%99%A4
插件后即可

解除网站复制限制

进入你想复制的网站,选中要复制的文本,进行复制即可。
如果还是不能复制,则安装其他脚本试试。

JavaScript格式化日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Date.prototype.formatDate = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
// 获取年份
// ①
if (/(y+)/i.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}

for (var k in o) {
// ②
if (new RegExp("(" + k + ")", "i").test(fmt)) {
fmt = fmt.replace(
RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
// var now = new Date();
// console.log(now.format("YYYY-MM-DD")); // 2021-01-11
// console.log(now.format("yyyy年MM月dd日"));
// console.log(now.format("MM/dd/yyyy"));
// console.log(now.format("yyyyMMdd"));
// console.log(now.format("yyyy-MM-dd hh:mm:ss"));

演示

java字符串数组去重复 算法描述

依次从原来的数组中取出元素,放入到缓存数组中。
每次放入之前先在缓存数组中查找该元素,如果缓存数组中已经有该元素了,就不放入缓存数组,如果没有,则放入缓存数组中。

实现方式

创建list集合,每次放入之前先在list里面查找,list里面没有则放入。
利用Set集合不能放入重复的元素的特性,直接放入Set集合。

下载安装ConnectBot app

GooglePlay Store:https://play.google.com/store/apps/details?id=org.connectbot&hl=zh&gl=US

ConnectBot中生成秘钥

点击app右上角的竖直三点按钮,选择管理秘钥,然后点击右上角的加号,输入秘钥的昵称(随意),选择转发类型(默认选RSA,默认就好),输入位数(默认2048默认就好),秘钥的密码(默认不设置,默认就好)然后勾选启动时载入秘钥。最后点击最下方的生成按钮,此时会弹出一个方框,在方框上面随意滑动,app会根据你的滑动生成随机数来生成秘钥。滑动到提示100%即可生产秘钥对。

复制ConnectBot生成的公钥

长按生成的公钥,选择复制公钥

添加主机

点击右下角的加号来添加主机,在协议输入框中输入用户名@主机名:端口号,例如root@192.168.1.3:22.
选择使用秘钥验证, 其他默认即可

添加公钥到/root/.ssh/authorized_keys文件中

进入CentOS,进入/root/.ssh目录:

1
2
3
[root@localhost ~]# cd /root/.ssh/
[root@localhost .ssh]# ls
id_rsa id_rsa.pub known_hosts

可以看到目录下没有authorized_keys文件,先创建该文件:

1
2
3
[root@localhost .ssh]# touch authorized_keys
[root@localhost .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts

编辑authorized_keys文件

1
[root@localhost .ssh]# vim authorized_keys

把复制到的公钥添加在文件末尾,添加效果:

1
2
3
[root@localhost .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCufeexdUy0gbKFczV9AedZPGaiidpWx8rh0UEcfCuSW6NJTVjEUvEiFGW5CR24g3Jf+rnX5cbffU+DwdT6nQJRdS3XnGqfpyrtef+98+rNAmoWk0nB1Uiby6BEgFZ8wUkqs5zO159BkoYRvsUjn51SomEgqsOXo52x50nvW0vgO445aohTYYQzm7h7UX5x/ZEJvHujf0pVjE0RMfDP/n3piQTVZwyuO064mR5YnDLs6Skg8klQX/iknYWxSkCYXGStKDbpUoGyvy3CvwJJHvtP4DhUcmnEi87u/UuFc/PTXK8hAnKS/a/hKDPm1b0jRGt0UFQJpws5ZAmcuKNgyr9L connerbot
[root@localhost .ssh]#

ConnectBot连接服务器

完成以上步骤以后,使用ConnectBot连接服务器时就不需要输入密码了。
在ConnectBot中点击主机即可连接。如果没有连上,可能你的ConnectBot中的秘钥还没载入内存,长按秘钥,将秘钥载入内存,然后再次连接主机即可。

参考资料

http://zduo.me/blog/?p=610

检查更新

1
npm outdated
1
2
3
4
5
6
7
8
9
lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/exam (master)
$ npm outdated
Package Current Wanted Latest Location
hexo 4.2.1 4.2.1 5.4.0 hexo-site
hexo-deployer-git 2.1.0 2.1.0 3.0.0 hexo-site
hexo-generator-search 2.4.0 2.4.1 2.4.1 hexo-site
hexo-generator-sitemap 2.0.0 2.1.0 2.1.0 hexo-site
hexo-renderer-stylus 1.1.0 1.1.0 2.0.1 hexo-site
hexo-server 1.0.0 1.0.0 2.0.0 hexo-site

我这里的hexo是4.2.1这已经是hexo4的最高版本了,我之前有试过升级到hexo5,但是会打不开站点。所以我就升级插件就行了,hexo就不更新了。
如果需要用hexo5的话,可能要重新使用hexo init创建一个新的站点,然后将原来的文章复制过去才行。

修改package.json

更新要升级的插件的版本号:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ git diff package.json
warning: LF will be replaced by CRLF in package.json.
The file will have its original line endings in your working directory
diff --git a/package.json b/package.json
index 499cb10..71c188c 100644
--- a/package.json
+++ b/package.json
@@ -15,17 +15,17 @@
"hexo": "^4.2.1",
"hexo-abbrlink": "^2.2.1",
"hexo-auto-category": "^0.2.0",
- "hexo-deployer-git": "^2.1.0",
+ "hexo-deployer-git": "^3.0.0",
"hexo-generator-archive": "^1.0.0",
"hexo-generator-baidu-sitemap": "^0.1.9",
"hexo-generator-category": "^1.0.0",
"hexo-generator-index-pin-top": "^0.2.2",
- "hexo-generator-search": "^2.4.0",
- "hexo-generator-sitemap": "^2.0.0",
+ "hexo-generator-search": "^2.4.1",
+ "hexo-generator-sitemap": "^2.1.0",
"hexo-generator-tag": "^1.0.0",
"hexo-renderer-ejs": "^1.0.0",
"hexo-renderer-kramed": "^0.1.4",
- "hexo-renderer-stylus": "^1.1.0",
- "hexo-server": "^1.0.0"
+ "hexo-renderer-stylus": "^2.0.1",
+ "hexo-server": "^2.0.0"
}
}

安装更新

1
npm install --save

从Git版本库中删除已经添加的文件package-lock.json

这个文件是npm install命令生成的,里面存放有插件的地址,没有必要加入版本库,我之前不知道,没有将该文件排除在外。

在.gitignore添加该文件

打开.gitignore文件,把package-lock.json添加到其中。

1
2
3
4
5
6
7
8
9
10
11
12
lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/exam (master)
$ git diff .gitignore
diff --git a/.gitignore b/.gitignore
index 122aa34..8b3df4c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,4 +8,5 @@ public/
备份.md
*/测试.md
*/测试/*
+package-lock.json
\ No newline at end of file

使用git rm –cached命令删除已经添加过的文件

.gitignore只对新添加的文件有效,由于package-lock.json文件已经在版本库中。所以此时.gitignore对该文件没有影响。
现在接着使用git rm –cached删除该文件即可:

git rm -n –cached

为了以防删错,可以多添加-n参数(git rm -n –cached),这样会显示你要删除的文件,但不会真正的删除。
等确认是你要删除的文件后,再去掉-n参数,使用git rm –cached 删除即可。

删除效果:

1
2
3
4
5
6
7
8
9
lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/exam (master)
$ git rm -n --cached package-lock.json
rm 'package-lock.json'

lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/exam (master)
$ git rm --cached package-lock.json
rm 'package-lock.json'

lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/exam (master)

查看删除效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/exam (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: package-lock.json

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: .gitignore
modified: package.json

下载git源码压缩包

从Git官网上下载

进入git下载网站:https://git-scm.com/download/linux
滚动页面到最下方:可以看到CentOS对应的安装方式。:

Red Hat Enterprise Linux, Oracle Linux, CentOS, Scientific Linux, et al.
RHEL and derivatives typically ship older versions of git. You can download a tarball and build from source, or use a 3rd-party repository such as the IUS Community Project to obtain a more recent version of git.

翻译:
RHEL及其衍生物通常提供较旧版本的git。您可以从源代码下载tarball并进行构建,或者使用第三方存储库(如IUS社区项目)来获取git的最新版本。

从GitHub上下载

https://github.com/git/git/releases

使用Xftp上传到centos上

先用Xshell连接手机,然后在Xshell中打开Xftp,把git的压缩包上传到/opt目录

解压Git源码包

1
tar -xzf git-2.29.3.tar.gz

运行效果

1
2
3
4
5
[root@localhost ]# cd /opt
[root@localhost opt]# mkdir git
[root@localhost opt]# ls
apache-tomcat-8.5.63.tar.gz git git-2.29.3.tar.gz hexo java jdk-8u281-linux-aarch64.tar.gz nvm nvm-0.37.2.tar.gz tomcat tomcat85
[root@localhost opt]# tar -xzf git-2.29.3.tar.gz -C git

进入解压后的git目录

1
2
3
4
5
6
7
8
9
10
[root@localhost opt]# ls
apache-tomcat-8.5.63.tar.gz git git-2.29.3.tar.gz hexo java jdk-8u281-linux-aarch64.tar.gz nvm nvm-0.37.2.tar.gz tomcat tomcat85
[root@localhost opt]# cd git
[root@localhost git]# ls
git-2.29.3
[root@localhost git]# cd git-2.29.3/
[root@localhost git-2.29.3]# ls
abspath.c connect.h git-mergetool.sh merge-blobs.h reachable.c submodule-config.h
aclocal.m4 contrib git-p4.py merge.c reachable.h submodule.h
......

安装Git在CentOS对应的依赖

1
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel

上面这个命令会自动安装旧版本的git,所以,安装之前,要先移除该git

移除旧版git

1
yum remove git

编译安装

移除旧版的git后,就可以执行厦门的两个命令进行编译和安装了:

1
2
make prefix=/usr/local/git all
make prefix=/usr/local/git install

编译和安装需要的时间可能比较长,耐心等待运行结束。

设置git的环境变量

1
vim /etc/profile

在文件末尾添加:

1
export PATH=$PATH:/usr/local/git/bin

然后刷新环境变量:

1
source /etc/profile

测试安装是否成功

1
git version

运行效果:

1
2
3
[root@localhost opt]# git --version
git version 2.29.3
[root@localhost opt]#

设置Git自动补全

git的源码包里面已经有自动补全的脚本了,配置到环境变量即可使用,具体操作方法如下:

获取git-completion.bash文件的绝对路径

该文件在git源码目录下的相对路径为:

1
contrib/completion/git-completion.bash

找到该文件,并获取该文件的绝对路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost git]# pwd
/opt/git
[root@localhost git]# cd git-2.29.3/
[root@localhost git-2.29.3]# cd contrib/
[root@localhost contrib]# ls
buildsystems coverage-diff.sh examples git-shell-commands mw-to-git remotes2config.sh thunderbird-patch-inline
coccinelle credential fast-import hg-to-git persistent-https rerere-train.sh update-unicode
completion diff-highlight git-jump hooks README stats vscode
contacts emacs git-resurrect.sh long-running-filter remote-helpers subtree workdir
[root@localhost contrib]# cd completion/
[root@localhost completion]# ls
git-completion.bash git-completion.tcsh git-completion.zsh git-prompt.sh
[root@localhost completion]# pwd
/opt/git/git-2.29.3/contrib/completion
[root@localhost completion]# readlink -f git-completion.bash
/opt/git/git-2.29.3/contrib/completion/git-completion.bash
[root@localhost completion]#

好的最终我得到的绝对路径为:

1
/opt/git/git-2.29.3/contrib/completion/git-completion.bash

设置环境变量

1
vim ~/.bashrc

在该环境变量文件的末尾最后加入下面内容

1
source git-completion.bash文件的绝对路径

我这里是:

1
source /opt/git/git-2.29.3/contrib/completion/git-completion.bash

运行效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost opt]# vim ~/.bashrc 
[root@localhost opt]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
......
# Git自动补全脚本环境变量
source /opt/git/git-2.29.3/contrib/completion/git-completion.bash
[root@localhost opt]#

完成以上步骤后,重启 shell,就可以通过 tab 键自动补全 Git 命令了。
例如输入git s然后按tab键,会有如下提示信息:

1
2
3
4
[root@localhost blog]# git s
send-email show sparse-checkout stash submodule
shortlog show-branch stage status switch
[root@localhost blog]# git s

参考资料

https://www.runoob.com/git/git-install-setup.html
https://ehlxr.me/2016/07/30/CentOS-7-%E5%AE%89%E8%A3%85%E6%9C%80%E6%96%B0%E7%9A%84-Git/
https://my.oschina.net/mobinchao/blog/3023355

https://ehlxr.me/2016/09/04/CentOS-%E4%B8%AD%E9%85%8D%E7%BD%AE-Git-%E5%91%BD%E4%BB%A4%E8%87%AA%E5%8A%A8%E8%A1%A5%E5%85%A8/
https://www.jianshu.com/p/b3531cf9bd0e

LinuxDeploy CentOS7升级Git版本

注意!本文并没能升级成功,IUS老是报错404,所以本文对你可能没有帮助,我留着这篇文章只是做个记录而已。

使用EPEL库

EPEL (Extra Packages for Enterprise Linux) 想必大多数人都用过,它是一个免费、开源,广受欢迎的,基于社区的库项目。其目标是提供一个在Fedora下开发、测试和完善的高质量的软件包。并且可以在RHEL, CentOS和Scientific Linux等Linux系统可用。
如果想在你的系统上使用EPEL库,使用如下命令:

1
yum install epel-release

IUS Community 库

IUS (Inline with Upstream Stable) ,它是一个新的第三方的、社区支持的库,它为PHP, Python和MySQL提供了最新的高质量的RPM包。
IUS依赖EPEL,所以要先安装epel-release.
安装IUS库:

1
2
3
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

卸载旧版的git

1
yum remove git

安装新版的git

1
yum -y install  git2u-all

我安装失败了,报错如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[root@localhost ~]# yum install git2u
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
Could not get metalink https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=aarch64&infra=stock&content=altarch error was
12: Timeout on https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=aarch64&infra=stock&content=altarch: (28, 'Operation timed out after 30001 milliseconds with 0 out of 0 bytes received')
* epel: mirrors.bfsu.edu.cn
base | 3.6 kB 00:00:00
centos-7-aarch64 | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
https://repo.ius.io/7/aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
正在尝试其它镜像。
To address this issue please refer to the below wiki article

https://wiki.centos.org/yum-errors

If above article doesn't help to resolve this issue please use https://bugs.centos.org/.



One of the configured repositories failed (IUS for Enterprise Linux 7 - aarch64),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:

1. Contact the upstream for the repository and get them to fix the problem.

2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).

3. Run the command with the repository temporarily disabled
yum --disablerepo=ius ...

4. Disable the repository permanently, so yum won't use it by default. Yum
will then just ignore the repository until you permanently enable it
again or use --enablerepo for temporary usage:

yum-config-manager --disable ius
or
subscription-manager repos --disable=ius

5. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:

yum-config-manager --save --setopt=ius.skip_if_unavailable=true

failure: repodata/repomd.xml from ius: [Errno 256] No more mirrors to try.
https://repo.ius.io/7/aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found

如何卸载EPEL库

我不知道具体怎么卸载IUS库的方法,下面是下载EPEL库的方法。

1
yum remove epel-release

运行效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[root@localhost ~]# yum remove epel-release
已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 epel-release.noarch.0.7-13 将被 删除
--> 正在处理依赖关系 epel-release = 7,它被软件包 ius-release-2-1.el7.ius.noarch 需要
--> 正在检查事务
---> 软件包 ius-release.noarch.0.2-1.el7.ius 将被 删除
--> 解决依赖关系完成

依赖关系解决

======================================================================================================================================================================
Package 架构 版本 源 大小
======================================================================================================================================================================
正在删除:
epel-release noarch 7-13 @/epel-release-latest-7.noarch 25 k
为依赖而移除:
ius-release noarch 2-1.el7.ius @/ius-release-el7 4.5 k

事务概要
======================================================================================================================================================================
移除 1 软件包 (+1 依赖软件包)

安装大小:29 k
是否继续?[y/N]:y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在删除 : ius-release-2-1.el7.ius.noarch 1/2
正在删除 : epel-release-7-13.noarch 2/2
验证中 : ius-release-2-1.el7.ius.noarch 1/2
验证中 : epel-release-7-13.noarch 2/2

删除:
epel-release.noarch 0:7-13

作为依赖被删除:
ius-release.noarch 0:2-1.el7.ius

完毕!
[root@localhost ~]#

如何卸载IUS库

那我要是没猜错的话,卸载IUS库应该和上面卸载EPEL库的命令yum remove epel-release差不多,应该是:

1
yum remove ius-release

参考资料

CentOS和RedHat下8个最常用的YUM库:https://zhuanlan.zhihu.com/p/57154163
Centos7升级git:https://zhuanlan.zhihu.com/p/73357795
https://ius.io/
https://ius.io/setup