Post

linux(CentOS7) 文件管理与磁盘管理

文件管理

认识分区

1
2
3
4
5
6
[root@Cc ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        909M     0  909M   0% /dev
[root@Cc ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        909M     0  909M    0% /dev

文件管理

文件类型

  • 普通文件 -
  • 目录文件 d
  • 块设备文件 d/c
  • 连接文件
  • 字符设备文件 d/c
  • socket 文件 s
  • 管道文件 p

文件属性权限

  • 读…………………………r
  • 写…………………………w
  • 可执行…………………X
示例
1
2
3
4
5
-rwxrw-r--
[-]表示普通文件
[rwx]表示读写执行权限
[rw-]所属与同一组的用户就有读写权限
[r--]表示其他用户对该文件只有读权限

-rwxrwxrx:为文件最大权限,对应的编码为 777,表示任何用户都有读写执行此文件

改变文件所有权

  • chown
  • chgrp

改变文件权限

chmod
  • r—-4—–读———read
  • w—2—–写——–write
  • x—-1—–执行—–[execute]
示例
1
2
3
4
5
6
7
[root@Cc 5]# touch test.sh
[root@Cc 5]# ll
总用量 0
-rw-r--r-- 1 root root 0 3月  26 22:02 test.sh
[root@Cc 5]# ./test.sh
-bash: ./test.sh: 权限不够
[root@Cc 5]#

test.sh没有执行权限

1
2
[root@Cc 5]# chmod u+x test.sh
[root@Cc 5]# ./test.sh

加上权限

1
2
3
4
5
[root@Cc 5]# chmod o-r test.sh
[root@Cc 5]# ll
总用量 0
-rwxr----- 1 root root 0 3月  26 22:02 test.sh
[root@Cc 5]#

设置其他用户不可读

1
2
3
4
5
6
7
8
9
10
[root@Cc 5]# ll
总用量 0
-rwxr----- 1 root root 0 3月  26 22:02 test.sh
-rwxrwxr-x 1 root root 0 3月  26 22:11 test.txt
[root@Cc 5]# chmod ugo+r test.txt
[root@Cc 5]# chmod a+r test.txt
[root@Cc 5]# chmod ug+w,o-w test.txt
[root@Cc 5]# chmod u+x test.txt
[root@Cc 5]# chmod -R a+r *
[root@Cc 5]# chmod a-x test.txt
  • 设置所有人可读
  • 设置所有人可读
  • 设置文件拥有者,其他所属群体可写入,其他用户则不能
  • 设置该文件用者可执行
  • 讲目录下所有文件子目录都设置为可读取
  • 收回所有用户对test.txt执行权限

磁盘管理

查看磁盘占用空间

df -ah 其中h是单位有k

查看文件或者目录所占用空间

du

示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@Cc lichaocheng]# du -sh
1.9G    .
[root@Cc lichaocheng]# du --max-depth=1 -h
4.0K    ./vimTest
78M     ./gogs
181M    ./java
130M    ./sonsul
85M     ./rabbitmq
1.2G    ./harbor
6.5M    ./hugo
112K    ./hf
80K     ./sll
11M     ./frp
24K     ./file
20K     ./C
16M     ./frp1
16K     ./ssl
111M    ./mongodb
1.9G    .
[root@Cc lichaocheng]#
  • 统计大小
  • 按照层级文件大小

一些冷门命令

  • tune2fs(调整查看文件系统参数)
  • mkfs(格式化文件系统)

挂在/卸载文件系统

mount 如果在 windows、mac 、或者自己电脑装了 linux 就可以使用到这个命令,这个只给你看下帮助文档

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
[root@Cc lichaocheng]# mount --help;

用法:
 mount [-lhV]
 mount -a [选项]
 mount [选项] [--source] <源> | [--target] <目录>
 mount [选项] <源> <目录>
 mount <操作> <挂载点> [<目标>]

选项:
 -a, --all               挂载 fstab 中的所有文件系统
 -c, --no-canonicalize   不对路径规范化
 -f, --fake              空运行;跳过 mount(2) 系统调用
 -F, --fork              对每个设备禁用 fork(-a 选项一起使用)
 -T, --fstab <路径>      /etc/fstab 的替代文件
 -h, --help              显示此帮助并退出
 -i, --internal-only     不调用 mount.<类型> 助手程序
 -l, --show-labels       列出所有带有指定标签的挂载
 -n, --no-mtab           不写 /etc/mtab
 -o, --options <列表>    挂载选项列表,以英文逗号分隔
 -O, --test-opts <列表>  限制文件系统集合(-a 选项一起使用)
 -r, --read-only         以只读方式挂载文件系统(-o ro)
 -t, --types <列表>      限制文件系统类型集合
     --source <源>       指明源(路径、标签、uuid)
     --target <目标>     指明挂载点
 -v, --verbose           打印当前进行的操作
 -V, --version           显示版本信息并退出
 -w, --rw, --read-write  以读写方式挂载文件系统(默认)

 -h, --help     显示此帮助并退出
 -V, --version  输出版本信息并退出

源:
 -L, --label <标签>      同 LABEL=<label>
 -U, --uuid <uuid>       同 UUID=<uuid>
 LABEL=<标签>            按文件系统标签指定设备
 UUID=<uuid>             按文件系统 UUID 指定设备
 PARTLABEL=<标签>        按分区标签指定设备
 PARTUUID=<uuid>         按分区 UUID 指定设备
 <设备>                  按路径指定设备
 <目录>                  绑定挂载的挂载点(参阅 --bind/rbind)
 <文件>                  用于设置回环设备的常规文件

操作:
 -B, --bind              挂载其他位置的子树(-o bind)
 -M, --move              将子树移动到其他位置
 -R, --rbind             挂载其他位置的子树及其包含的所有挂载
 --make-shared           将子树标记为 共享
 --make-slave            将子树标记为 从属
 --make-private          将子树标记为 私有
 --make-unbindable       将子树标记为 不可绑定
 --make-rshared          递归地将整个子树标记为 共享
 --make-rslave           递归地将整个子树标记为 从属
 --make-rprivate         递归地将整个子树标记为 私有
 --make-runbindable      递归地将整个子树标记为 不可绑定

基本磁盘管理

fdisk 类似 windows 下 PQMagic 工具 ,这个只给帮助文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@Cc lichaocheng]# fdisk --help
fdisk:无效选项 -- -
用法:
 fdisk [选项] <磁盘>    更改分区表
 fdisk [选项] -l <磁盘> 列出分区表
 fdisk -s <分区>        给出分区大小(块数)

选项:
 -b <大小>             扇区大小(512、1024、2048或4096)
 -c[=<模式>]           兼容模式:“dos”或“nondos”(默认)
 -h                    打印此帮助文本
 -u[=<单位>]           显示单位:“cylinders”(柱面)或“sectors”(扇区,默认)
 -v                    打印程序版本
 -C <数字>             指定柱面数
 -H <数字>             指定磁头数
 -S <数字>             指定每个磁道的扇区数

交换空间管理

磁盘冗余阵列 RAID

监控硬盘空间(shell)是个联系暂时不写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/sh
function LOG()
{
	echo "用于记录执行日志"
}
function sendmsg()
{
	echo "告警发生详细"
}
function process()
{
	echo "主处理逻辑"
}
function main()
{
	process
}
LOG "process start"
main
LOG "process end"
This post is licensed under CC BY 4.0 by the author.