文件系统中的设备
GUI显示磁盘占用情况分析工具:kdirstat
安装:sudo apt-get install kdirstat
,运行:k4dirstat
。
用方块大小显示每个文件磁盘占用情况。
-
df
统计磁盘分区 -
du
统计目录内容。
df
统计磁盘分区
df -T # 显示格式 df -a # 显示所有文件系统的磁盘使用情况,包括0块(block)的文件系统,如/proc文件系统。 df -k # 以k字节为单位显示。 df -i # 显示i节点信息,而不是磁盘块。 df -t # 显示各指定类型的文件系统的磁盘空间使用情况。 df -x # 列出不是某一指定类型文件系统的磁盘空间使用情况(与t选项相反)。
报告系统的磁盘使用情况。在跟踪磁盘使用情况方面对于普通用户和系统管理员都很有用。
df
通过检查目录大小工作,但这一数值仅当文件关闭时才得到更新。
Mandatory arguments to long options are mandatory for short options too.
-
-a
,--all
include dummy file systems -
-B
,--block-size=SIZE
use SIZE-byte blocks -
-h
,--human-readable
print sizes in human readable format (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) -
-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
SIZE
may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.
查看使用多少空间
The df
command displays the information of device name, total blocks, total
disk space, used disk space, available disk space and mount points on a file
system.
[root@tecmint ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 78361192 23185840 51130588 32% / /dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 295561 21531 258770 8% /boot tmpfs 257476 0 257476 0% /dev/shm
查看全部的分区使用空间。 The same as above, but it also displays information of dummy file systems along with all the file system disk usage and their memory utilization.
[root@tecmint ~]# df -a Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 78361192 23186116 51130312 32% / proc 0 0 0 - /proc sysfs 0 0 0 - /sys devpts 0 0 0 - /dev/pts /dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 295561 21531 258770 8% /boot tmpfs 257476 0 257476 0% /dev/shm none 0 0 0 - /proc/sys/fs/binfmt_misc sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
查询/home
分区的情况:
[root@tecmint ~]# df -hT /home Filesystem Type Size Used Avail Use% Mounted on /dev/cciss/c0d0p5 ext3 24G 22G 1.2G 95% /home
按指定单位显示空间大小
Display Information of File System in Bytes:
To display all file system information and usage in 1024-byte blocks, use the
option -k
(e.g. –block-size=1K
) as follows.
[root@tecmint ~]# df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 78361192 23187212 51129216 32% / /dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 295561 21531 258770 8% /boot tmpfs 257476 0 257476 0% /dev/shm
-m
Display Information of File System in MB:
[root@tecmint ~]# df -m Filesystem 1M-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 76525 22644 49931 32% / /dev/cciss/c0d0p5 24217 21752 1215 95% /home /dev/cciss/c0d0p3 29057 24907 2651 91% /data /dev/cciss/c0d0p1 289 22 253 8% /boot tmpfs 252 0 252 0% /dev/shm
-h
返回结果的可读性,自动显示单位:
[root@tecmint ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/cciss/c0d0p2 75G 23G 49G 32% / /dev/cciss/c0d0p5 24G 22G 1.2G 95% /home /dev/cciss/c0d0p3 29G 25G 2.6G 91% /data /dev/cciss/c0d0p1 289M 22M 253M 8% /boot tmpfs 252M 0 252M 0% /dev/shm
显示inode节点
Using -i
switch will display the information of number of used inodes and
their percentage for the file system.
[root@tecmint ~]# df -i Filesystem Inodes IUsed IFree IUse% Mounted on /dev/cciss/c0d0p2 20230848 133143 20097705 1% / /dev/cciss/c0d0p5 6403712 798613 5605099 13% /home /dev/cciss/c0d0p3 7685440 1388241 6297199 19% /data /dev/cciss/c0d0p1 76304 40 76264 1% /boot tmpfs 64369 1 64368 1% /dev/shm
显示磁盘分区格式
-T
显示格式:
[root@tecmint ~]# df -T Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 ext3 78361192 23188812 51127616 32% / /dev/cciss/c0d0p5 ext3 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 ext3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 ext3 295561 21531 258770 8% /boot tmpfs tmpfs 257476 0 257476 0% /dev/shm
-t
显示指定格式的分区:
[root@tecmint ~]# df -t ext3 Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 78361192 23190072 51126356 32% / /dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 295561 21531 258770 8% /boot
-x
不显示指定的类型的分区:
[root@tecmint ~]# df -x ext3 Filesystem 1K-blocks Used Available Use% Mounted on tmpfs 257476 0 257476 0% /dev/shm
du
统计目录内容
估计文件的空间占用。 逐层统计文件(例如以递归方式)并输出摘要。
root@tecmint:~# du 8 ./Daily Pics/wp-polls/../wiki_images/default_gradient 8 ./Daily Pics/wp-polls/../wiki_images/default 32 ./Daily Pics/wp-polls/images 8 ./Daily Pics/wp-polls/tinymce/plugins/polls/langs 8 ./Daily Pics/wp-polls/tinymce/plugins/polls/img 28 ./Daily Pics/wp-polls/tinymce/plugins/polls 32 ./Daily Pics/wp-polls/tinymce/plugins 36 ./Daily Pics/wp-polls/tinymce 580 ./Daily Pics/wp-polls 1456 ./Daily Pics 36 ./Plugins/wordpress-author-box 16180 ./Plugins 12 ./May Articles 2013/Xtreme Download Manager 4632 ./May Articles 2013/XCache
注意: df
只显示文件系统的使用统计,但du
统计目录内容。
查看使用情况:
To find out the disk usage summary of a /home/tecmint
directory tree and each
of its sub directories. Enter the command as:
[root@tecmint]# du /home/tecmint 40 /home/tecmint/downloads 4 /home/tecmint/.mozilla/plugins 4 /home/tecmint/.mozilla/extensions 12 /home/tecmint/.mozilla 12 /home/tecmint/.ssh 689112 /home/tecmint/Ubuntu-12.10 689360 /home/tecmint
The output of the above command displays the number of disk blocks in the
/home/tecmint
directory along with its sub-directories.
-h
增加可读性:
[root@tecmint]# du -h /home/tecmint 40K /home/tecmint/downloads 4.0K /home/tecmint/.mozilla/plugins 4.0K /home/tecmint/.mozilla/extensions 12K /home/tecmint/.mozilla 12K /home/tecmint/.ssh 673M /home/tecmint/Ubuntu-12.10 674M /home/tecmint
-s
指定目录:
[root@tecmint]# du -sh /home/tecmint 674M /home/tecmint
-a
所有文件与目录:
[root@tecmint]# du -a /home/tecmint 4 /home/tecmint/.bash_logout 12 /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz 24 /home/tecmint/downloads/Phpfiles-org.tar.bz2 40 /home/tecmint/downloads 12 /home/tecmint/uploadprogress-1.0.3.1.tgz 4 /home/tecmint/.mozilla/plugins 4 /home/tecmint/.mozilla/extensions 12 /home/tecmint/.mozilla 4 /home/tecmint/.bashrc 689108 /home/tecmint/Ubuntu-12.10/ubuntu-12.10-server-i386.iso 689112 /home/tecmint/Ubuntu-12.10 689360 /home/tecmint
-a
flag along with -h
:
[root@tecmint]# du -ah /home/tecmint 4.0K /home/tecmint/.bash_logout 12K /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz 24K /home/tecmint/downloads/Phpfiles-org.tar.bz2 40K /home/tecmint/downloads 12K /home/tecmint/uploadprogress-1.0.3.1.tgz 4.0K /home/tecmint/.mozilla/plugins 4.0K /home/tecmint/.mozilla/extensions 12K /home/tecmint/.mozilla 4.0K /home/tecmint/.bashrc 673M /home/tecmint/Ubuntu-12.10/ubuntu-12.10-server-i386.iso 673M /home/tecmint/Ubuntu-12.10 674M /home/tecmint
Find out the disk usage of a directory tree with its subtress in Kilobyte blcoks
. Use the -k
(displays size in 1024 bytes units).
[root@tecmint]# du -k /home/tecmint 40 /home/tecmint/downloads 4 /home/tecmint/.mozilla/plugins 4 /home/tecmint/.mozilla/extensions 12 /home/tecmint/.mozilla 12 /home/tecmint/.ssh 689112 /home/tecmint/Ubuntu-12.10 689360 /home/tecmint
along with its subtrees in Megabytes
To get the summary of disk usage of directory tree along with its subtrees in
Megabytes (MB) only. Use the option -mh
as follows. The -m
flag counts the
blocks in MB units and -h
stands for human readable format.
[root@tecmint]# du -mh /home/tecmint 40K /home/tecmint/downloads 4.0K /home/tecmint/.mozilla/plugins 4.0K /home/tecmint/.mozilla/extensions 12K /home/tecmint/.mozilla 12K /home/tecmint/.ssh 673M /home/tecmint/Ubuntu-12.10 674M /home/tecmint
-c
在最后一行加上所有目录的汇总
[root@tecmint]# du -ch /home/tecmint 40K /home/tecmint/downloads 4.0K /home/tecmint/.mozilla/plugins 4.0K /home/tecmint/.mozilla/extensions 12K /home/tecmint/.mozilla 12K /home/tecmint/.ssh 673M /home/tecmint/Ubuntu-12.10 674M /home/tecmint 674M total
--exclude
排除指定的文件
[root@tecmint]# du -ah --exclude="*.txt" /home/tecmint 4.0K /home/tecmint/.bash_logout 12K /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz 24K /home/tecmint/downloads/Phpfiles-org.tar.bz2 40K /home/tecmint/downloads 12K /home/tecmint/uploadprogress-1.0.3.1.tgz 4.0K /home/tecmint/.bash_history 4.0K /home/tecmint/.bash_profile 4.0K /home/tecmint/.mozilla/plugins 4.0K /home/tecmint/.mozilla/extensions 12K /home/tecmint/.mozilla 4.0K /home/tecmint/.bashrc 24K /home/tecmint/Phpfiles-org.tar.bz2 4.0K /home/tecmint/geoipupdate.sh 4.0K /home/tecmint/.zshrc 120K /home/tecmint/goaccess-0.4.2.tar.gz.1 673M /home/tecmint/Ubuntu-12.10/ubuntu-12.10-server-i386.iso 673M /home/tecmint/Ubuntu-12.10 674M /home/tecmint
–time
显示修改时间:
[root@tecmint]# du -ha --time /home/tecmint 4.0K 2012-10-12 22:32 /home/tecmint/.bash_logout 12K 2013-01-19 18:48 /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz 24K 2013-01-19 18:48 /home/tecmint/downloads/Phpfiles-org.tar.bz2 40K 2013-01-19 18:48 /home/tecmint/downloads 12K 2013-01-19 18:32 /home/tecmint/uploadprogress-1.0.3.1.tgz 4.0K 2012-10-13 00:11 /home/tecmint/.bash_history 4.0K 2012-10-12 22:32 /home/tecmint/.bash_profile 0 2013-01-19 18:32 /home/tecmint/xyz.txt 0 2013-01-19 18:32 /home/tecmint/abc.txt 4.0K 2012-10-12 22:32 /home/tecmint/.mozilla/plugins 4.0K 2012-10-12 22:32 /home/tecmint/.mozilla/extensions 12K 2012-10-12 22:32 /home/tecmint/.mozilla 4.0K 2012-10-12 22:32 /home/tecmint/.bashrc 24K 2013-01-19 18:32 /home/tecmint/Phpfiles-org.tar.bz2 4.0K 2013-01-19 18:32 /home/tecmint/geoipupdate.sh 4.0K 2012-10-12 22:32 /home/tecmint/.zshrc 120K 2013-01-19 18:32 /home/tecmint/goaccess-0.4.2.tar.gz.1 673M 2013-01-19 18:51 /home/tecmint/Ubuntu-12.10/ubuntu-12.10-server-i386.iso 673M 2013-01-19 18:51 /home/tecmint/Ubuntu-12.10 674M 2013-01-19 18:52 /home/tecmint
lsof
使用中的文件
lsof
是"list open files("列表中打开的文件") 的缩写,显示您的系统当前已打开的
所有文件。
lsof通常的输出格式为:
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
常见包含例如以下几个字段
-
COMMAND
:默认以9个字符长度显示的命令名称。可使用+c參数指定显示的宽度,若+c后跟的參数为零,则显示命令的全名 -
PID
:进程的ID号 -
PPID
:父进程的IP号,默认不显示,当使用-R參数可打开。 -
PGID
:进程组的ID编号,默认也不会显示,当使用-g參数时可打开。 -
USER
:命令的执行UID或系统中登陆的username称。默认显示为username,当使用-l參数时,可显示UID。 -
FD
:是文件的File Descriptor number。
显示全部打开的文件:
# lsof COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME init 1 root cwd DIR 253,0 4096 2 / init 1 root rtd DIR 253,0 4096 2 / init 1 root txt REG 253,0 145180 147164 /sbin/init init 1 root mem REG 253,0 1889704 190149 /lib/libc-2.12.so init 1 root 0u CHR 1,3 0t0 3764 /dev/null init 1 root 1u CHR 1,3 0t0 3764 /dev/null init 1 root 2u CHR 1,3 0t0 3764 /dev/null init 1 root 3r FIFO 0,8 0t0 8449 pipe init 1 root 4w FIFO 0,8 0t0 8449 pipe init 1 root 5r DIR 0,10 0 1 inotify init 1 root 6r DIR 0,10 0 1 inotify init 1 root 7u unix 0xc1513880 0t0 8450 socket
Sections and it’s values are self-explanatory. However, we’ll review FD & TYPE columns more precisely.
FD – stands for File descriptor and may seen some of the values as:
-
cwd
current working directory -
rtd
root directory -
txt
program text (code and data) -
mem
memory-mapped file
Also in FD
column numbers like 1u is actual file descriptor and followed by
u,r,w of it’s mode as:
-
r
for read access. -
w
for write access. -
u
for read and write access.
TYPE
– of files and it’s identification.
-
DIR
– Directory -
REG
– Regular file -
CHR
– Character special file. -
FIFO
– First In First Out
按进程号查找: The below example only shows whose PID is 1 [One].
# lsof -p 1 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME init 1 root cwd DIR 253,0 4096 2 / init 1 root rtd DIR 253,0 4096 2 / init 1 root txt REG 253,0 145180 147164 /sbin/init init 1 root mem REG 253,0 1889704 190149 /lib/libc-2.12.so init 1 root mem REG 253,0 142472 189970 /lib/ld-2.12.so
inux删除文件后,被删除文件所占用的空间未被释放
lsof | grep -i deleted
-i
查找来自网络的操作
The following command with option -i
shows the list of all network connections
‘LISTENING & ESTABLISHED’.
# lsof -i COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954 rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN) avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600 rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN) cupsd 1346 root 6u IPv6 12112 0t0 TCP localhost:ipp (LISTEN) cupsd 1346 root 7u IPv4 12113 0t0 TCP localhost:ipp (LISTEN) sshd 1471 root 3u IPv4 12683 0t0 TCP *:ssh (LISTEN) master 1551 root 12u IPv4 12896 0t0 TCP localhost:smtp (LISTEN) master 1551 root 13u IPv6 12898 0t0 TCP localhost:smtp (LISTEN) sshd 1834 root 3r IPv4 15101 0t0 TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED) httpd 1918 root 5u IPv6 15991 0t0 TCP *:http (LISTEN) httpd 1918 root 7u IPv6 15995 0t0 TCP *:https (LISTEN) clock-app 2362 narad 21u IPv4 22591 0t0 TCP 192.168.0.2:45284->www.gov.com:http (CLOSE_WAIT) chrome 2377 narad 61u IPv4 25862 0t0 TCP 192.168.0.2:33358->maa03s04-in-f3.1e100.net:http (ESTABLISHED) chrome 2377 narad 80u IPv4 25866 0t0 TCP 192.168.0.2:36405->bom03s01-in-f15.1e100.net:http (ESTABLISHED)
按用户查找
显示指定用户打开的文件:
# lsof -u tecmint COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 1838 tecmint cwd DIR 253,0 4096 2 / sshd 1838 tecmint rtd DIR 253,0 4096 2 / sshd 1838 tecmint txt REG 253,0 532336 188129 /usr/sbin/sshd sshd 1838 tecmint mem REG 253,0 19784 190237 /lib/libdl-2.12.so sshd 1838 tecmint mem REG 253,0 122436 190247 /lib/libselinux.so.1 sshd 1838 tecmint mem REG 253,0 255968 190256 /lib/libgssapi_krb5.so.2.2 sshd 1838 tecmint mem REG 253,0 874580 190255 /lib/libkrb5.so.3.3
显示用户打开文件的命令:
# lsof -i -u tecmint COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 1839 tecmint cwd DIR 253,0 12288 15 /etc ping 2525 tecmint cwd DIR 253,0 12288 15 /etc
杀掉指定用户有打开文件的进程。 Sometimes you may have to kill all the processes for a specific user. Below command will kills all the processes of tecmint user.
# kill -9 `lsof -t -u tecmint`
按协议和端口查找
显示指定端口进程打开的文件:
# lsof -i TCP:22 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 1471 root 3u IPv4 12683 0t0 TCP *:ssh (LISTEN) sshd 1471 root 4u IPv6 12685 0t0 TCP *:ssh (LISTEN)
显示IPv4或IPv6 打开的文件:
# lsof -i 4 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954 rpcbind 1203 rpc 8u IPv4 11331 0t0 TCP *:sunrpc (LISTEN) avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600
# lsof -i 6 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 9u IPv6 11333 0t0 UDP *:sunrpc rpcbind 1203 rpc 10u IPv6 11335 0t0 UDP *:954 rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN) rpc.statd 1277 rpcuser 10u IPv6 11858 0t0 UDP *:55800 rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN) cupsd 1346 root 6u IPv6 12112 0t0 TCP localhost:ipp (LISTEN)
按协议与端口范围查找:
# lsof -i TCP:1-1024 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN) cupsd 1346 root 7u IPv4 12113 0t0 TCP localhost:ipp (LISTEN) sshd 1471 root 4u IPv6 12685 0t0 TCP *:ssh (LISTEN) master 1551 root 13u IPv6 12898 0t0 TCP localhost:smtp (LISTEN) sshd 1834 root 3r IPv4 15101 0t0 TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED) sshd 1838 tecmint 3u IPv4 15101 0t0 TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED) sshd 1871 root 3r IPv4 15842 0t0 TCP 192.168.0.2:ssh->192.168.0.1:groove (ESTABLISHED) httpd 1918 root 5u IPv6 15991 0t0 TCP *:http (LISTEN) httpd 1918 root 7u IPv6 15995 0t0 TCP *:https (LISTEN)
用符号^
排除条件
# lsof -i -u^root COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954 rpcbind 1203 rpc 8u IPv4 11331 0t0 TCP *:sunrpc (LISTEN) rpcbind 1203 rpc 9u IPv6 11333 0t0 UDP *:sunrpc rpcbind 1203 rpc 10u IPv6 11335 0t0 UDP *:954 rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN) avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600 rpc.statd 1277 rpcuser 5r IPv4 11836 0t0 UDP *:soap-beep rpc.statd 1277 rpcuser 8u IPv4 11850 0t0 UDP *:55146 rpc.statd 1277 rpcuser 9u IPv4 11854 0t0 TCP *:32981 (LISTEN) rpc.statd 1277 rpcuser 10u IPv6 11858 0t0 UDP *:55800 rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN)