linux中查看磁盘空间的方法举例

linux中查看磁盘空间的方法举例,第1张

linux中查看磁盘空间的方法举例查看当前目录大小:Java代码

[root@typengine.com]#

du

-sh

/var/www

查看指定目录大小:Java代码

[root@typengine.com]#

du

-sh

/www/typengine.com

查看当前目录文件总数:Java代码

[root@typengine.com]#

find

.

-type

f

|wc

-l

查看指定目录文件总数:Java代码

[root@typengine.com]#

find

/var/www

-type

f

|wc

-l

查看当前目录的目录总数:Java代码

[root@typengine.com]#

find

.

-type

d

|wc

-l

查看指定目录的目录总数:Java代码

[root@typengine.com]#

find

/www/typengine

-type

d

|wc

-l

以下内容供参考:Java代码

#

du

-ks

---in

all

#

du

-k

---every

last

du和df命令都被用于获得linux系统大小的信息:df用于报告文件系统的总块数及剩余块数,du

-s

/用于报告文件系统使用的块数。但是,我们可以发现从df命令算出的文件系统使用块数的值与通过du命令得出的值是不一致的。如下例:#

du

-s

/tmp

返回如下值:---12920

/tmp而

df

/tmp返回如下值:Java代码

Filesystem

--512-blocks--

Free

--%Used

--Iused--

%Iused

--Mounted

on

/dev/hd3

--------57344

--42208---

26%

----391

------4%

--/tmp

从上面的值我们可以算出

-

=

:

57344

-

42208

=

15136.而15136大于12920。该值差异的存在是由于du与df命令实施上的不同:

du

-s命令通过将指定文件系统中所有的目录、符号链接和文件使用的块数累加得到该文件系统使用的总块数;而df命令通过查看文件系统磁盘块分配图得出总块数与剩余块数。文件系统分配其中的一些磁盘块用来记录它自身的一些数据,如i节点,磁盘分布图,间接块,超级块等。这些数据对大多数用户级的程序来说是不可见的,通常称为Meta

Data。du命令是用户级的程序,它不考虑Meta

Data,而df命令则查看文件系统的磁盘分配图并考虑Meta

Data。df命令获得真正的文件系统数据,而du命令只查看文件系统的部分情况。例如,一个frag=4096

并且

nbpi=4096的空的大小为4MB的日志文件系统中Meta

Data的分配情况如下:Java代码

1

4k

block

for

the

LVM

2

4k

super

blocks

2

4k

blocks

for

disk

maps

2

4k

blocks

for

inode

maps

2

4k

blocks

for

.indirect

32

4k

blocks

for

inodes

-------------------------

41

4k

blocks

for

meta

data

on

an

empty

4MB

file

system

对于AIX

4.X版本:执行

du

/foo返回的结果如下:Java代码

----8

-------/foo/lost+found

----16

------/foo

要使du命令输出的结果与df命令输出的结果匹配,我们必须要加上Meta

Data。首先,将41个4k的块转换为以512字节为单位的值:Java代码

41

*

8

=

328

328(meta

data)

+

16(from

du)

=

344

所以有344个以512字节为单位的块分配给了这个空的文件系统。而使用

df

/foo命令我们可以得到下面的结果:Java代码

Filesystem

--512-blocks

--Free

--%Used

--Iused---%Iused

--Mounted

on

/dev/lv01

------8192

-----7848

-----5%

-----16

-----2%

----/foo

从中我们可以得到该文件系统使用的块数:Java代码

8192(total

blocks)

-

7848(free

blocks)

=

344。

该值与上面得出的值一致。上面的换算方法对于空的文件系统很容易实现,但是对于非空的文件系统,由于Meta

Data中文件间接块的大小不定,因此较难实现。不需要查看du

df返回的值的匹配关系,而只需要了解du

-s命令返回的值反映了分配给文件及目录的磁盘块数,而df命令则反映了文件系统的实际分配情况。df命令反映的实际情况包含了用户数据(文件及目录)和Meta

Data。另一个表现出du与df命令不同之处的例子如下:如果用户删除了一个正在运行的应用所打开的某个目录下的文件,则du命令返回的值显示出减去了该文件后的目录的大小。但df命令并不显示减去该文件后的大小。直到该运行的应用关闭了这个打开的文件,df返回的值才显示出减去了该文件后的文件系统的使用情况。列出一个目录占用的空间1. du或du

-s或du

-kdu

-S

|

sort

-n

可以迅速发现那个目录是最大的。2. 用df可以看到已安装的文件系统的空间大小及剩余空间大小。3. quota

-v查看用户的磁盘空间信息,如果你用quota限制了用户空间大小的话。Java代码

boot

5746310

/boot

[blinux@localhost

test]$

du

-ks

/boot

5772

/boot

[blinux@localhost

test]$

du

-ms

/boot

6

/boot

3.显示目录的总大小及目录树中各文件夹的大小,并以较好的单位表示Java代码

[blinux@localhost

test]$

du

-h

/boot

308K

/boot/grub

5.7M

/boot

package com.cmmb.util

import java.io.*

/**

* linux 下cpu 内存 磁盘 jvm的使用监控

* @author avery_leo

*

*/

public class DiskSpace {

/**

* 获取cpu使用情况

* @return

* @throws Exception

*/

public double getcpuUsage() throws Exception {

double cpuUsed = 0

Runtime rt = Runtime.getRuntime()

Process p = rt.exec("top -b -n 1")// 调用系统的“top"命令

BufferedReader in = null

try {

in = new BufferedReader(new InputStreamReader(p.getInputStream()))

String str = null

String[] strArray = null

while ((str = in.readLine()) != null) {

int m = 0

if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&

strArray = str.split(" ")

for (String tmp : strArray) {

if (tmp.trim().length() == 0)

continue

if (++m == 9) {// 第9列为cpu的使用百分比(RedHat

cpuUsed += Double.parseDouble(tmp)

}

}

}

}

} catch (Exception e) {

e.printStackTrace()

} finally {

in.close()

}

return cpuUsed

}

/**

* 内存监控

* @return

* @throws Exception

*/

public double getMemUsage() throws Exception {

double menUsed = 0

Runtime rt = Runtime.getRuntime()

Process p = rt.exec("top -b -n 1")// 调用系统的“top"命令

BufferedReader in = null

try {

in = new BufferedReader(new InputStreamReader(p.getInputStream()))

String str = null

String[] strArray = null

while ((str = in.readLine()) != null) {

int m = 0

if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&

//

// System.out.println("------------------3-----------------")

strArray = str.split(" ")

for (String tmp : strArray) {

if (tmp.trim().length() == 0)

continue

if (++m == 10) {

// 9)--第10列为mem的使用百分比(RedHat 9)

menUsed += Double.parseDouble(tmp)

}

}

}

}

} catch (Exception e) {

e.printStackTrace()

} finally {

in.close()

}

return menUsed

}

/**

* 获取磁盘空间大小

*

* @return

* @throws Exception

*/

public double getDeskUsage() throws Exception {

double totalhd = 0

double usedhd = 0

Runtime rt = Runtime.getRuntime()

Process p = rt.exec("df -hl /home")//df -hl 查看硬盘空间

BufferedReader in = null

try {

in = new BufferedReader(new InputStreamReader(p.getInputStream()))

String str = null

String[] strArray = null

while ((str = in.readLine()) != null) {

int m = 0

strArray = str.split(" ")

for (String tmp : strArray) {

if (tmp.trim().length() == 0)

continue

++m

System.out.println("----tmp----" + tmp)

if (tmp.indexOf("G") != -1) {

if (m == 2) {

System.out.println("---G----" + tmp)

if (!tmp.equals("") &&!tmp.equals("0"))

totalhd += Double.parseDouble(tmp

.substring(0, tmp.length() - 1)) * 1024

}

if (m == 3) {

System.out.println("---G----" + tmp)

if (!tmp.equals("none") &&!tmp.equals("0"))

usedhd += Double.parseDouble(tmp.substring(

0, tmp.length() - 1)) * 1024

}

}

if (tmp.indexOf("M") != -1) {

if (m == 2) {

System.out.println("---M---" + tmp)

if (!tmp.equals("") &&!tmp.equals("0"))

totalhd += Double.parseDouble(tmp

.substring(0, tmp.length() - 1))

}

if (m == 3) {

System.out.println("---M---" + tmp)

if (!tmp.equals("none") &&!tmp.equals("0"))

usedhd += Double.parseDouble(tmp.substring(

0, tmp.length() - 1))

System.out.println("----3----" + usedhd)

}

}

}

}

} catch (Exception e) {

e.printStackTrace()

} finally {

in.close()

}

//上面写在userd和total写反了,懒得改了,就反着用了

System.out.println("----totalhd----" + usedhd)

System.out.println("----usedhd----" + totalhd)

return (totalhd / usedhd) * 100

}

public static void main(String[] args) throws Exception {

DiskSpace cpu = new DiskSpace()

System.out.println("---------------cpu used:" + cpu.getcpuUsage() + "%")

System.out.println("---------------mem used:" + cpu.getMemUsage() + "%")

System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%")

System.out.println("------------jvm监控----------------------")

Runtime lRuntime = Runtime.getRuntime()

System.out.println("--------------Free Momery:" + lRuntime.freeMemory()+"K")

System.out.println("--------------Max Momery:" + lRuntime.maxMemory()+"K")

System.out.println("--------------Total Momery:" + lRuntime.totalMemory()+"K")

System.out.println("---------------Available Processors :"

+ lRuntime.availableProcessors())

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7144788.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-02
下一篇2023-04-02

发表评论

登录后才能评论

评论列表(0条)

    保存