Linux下查看文件和文件夹大小和内存使用情况
目录
du -h --max-depth=1 /home/work/ 仅列出/home/work/ 目录下面所有的一级目录文件大小;
du -h --max-depth=1 /home/work/* 列出/home/work/ 下面所有一级目录的一级目录文件大小。
1 Linux下查看文件和文件夹大小
2 删除系统日志等
场景:在sts中执行自动部署时候maven提示No space left on device错误,后来经检查发现是磁盘空间满了,用下面的方法分析发现tomcat下面的logs目录占用了很大的空间,删除多余的日志问题解决!
1、Linux下查看文件和文件夹大小
当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择。
df 可以查看一级文件夹大小、使用比例、档案系统及其挂入点,但对文件却无能为力。
du 查询文件或文件夹的磁盘使用空间。
两者配合使用,非常有效。比如用df查看哪个一级目录过大,然后用df查看文件夹或文件的大小,如此便可迅速确定症结。
1.1、示例:
[work@test01 logs]$ df -h 查看根目录的磁盘使用情况
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 232G 138G 95G 60% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 793M 7.0G 10% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 2.0G 139M 1.9G 7% /boot
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/sdb 394G 150M 374G 1% /home/work/hdd1
tmpfs 1.6G 0 1.6G 0% /run/user/10000
[work@test01 logs]$ df -l
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/cl-root 243146756 143916136 99230620 60% /
devtmpfs 8122956 0 8122956 0% /dev
tmpfs 8133876 0 8133876 0% /dev/shm
tmpfs 8133876 811800 7322076 10% /run
tmpfs 8133876 0 8133876 0% /sys/fs/cgroup
/dev/sda1 2086912 141584 1945328 7% /boot
tmpfs 1626776 0 1626776 0% /run/user/0
/dev/sdb 412716456 152712 391575840 1% /home/work/hdd1
tmpfs 1626776 0 1626776 0% /run/user/10000
[work@test01 logs]$ du -h 查看当前目录的大小
40G .
[work@test01 logs]$ ll -h
total 40G
-rw-r--r--. 1 work work 10G May 14 12:51 access.log
-rw-r--r--. 1 work work 48M May 14 12:51 error403.log
-rw-r--r--. 1 work work 30G May 14 12:52 error.log
如果当前目录下文件和文件夹很多,使用不带参数du的命令,可以循环列出所有文件和文件夹所使用的空间。这对查看究竟是那个地方过大是不利的,所以得指定深入目录的层数,参数:--max-depth=,这是个极为有用的参数!
如下,注意使用“*”,可以得到文件的使用空间大小.
提醒:一向命令比linux复杂的FreeBSD,它的du命令指定深入目录的层数却是比linux简化,为 -d。
下面的命令与上面的命令有什么异同??强烈推荐下面命令:
du -h --max-depth=1 /home/work/ 仅列出/home/work/ 目录下面所有的一级目录文件大小;
du -h --max-depth=1 /home/work/* 列出/home/work/ 下面所有一级目录的一级目录文件大小。

1.2、df和du命令参数选项
$ df --help
Usage: df [OPTION]... [FILE]...
Show information about the file system on which each FILE resides,
or all file systems by default.Mandatory arguments to long options are mandatory for short options too.
-a, --all include dummy 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 exitDisplay values are in units of the first available SIZE from --block-size,
and the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).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).FIELD_LIST is a comma-separated list of columns to be included. Valid
field names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent',
'size', 'used', 'avail', 'pcent', 'file' and 'target' (see info page).GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'df invocation'$ du --help
Usage: du [OPTION]... [FILE]...
or: du [OPTION]... --files0-from=F
Summarize disk usage of each FILE, recursively for directories.Mandatory arguments to long options are mandatory for short options too.
-0, --null end each output line with 0 byte rather than newline
-a, --all write counts for all files, not just directories
--apparent-size print apparent sizes, rather than disk usage; although
the apparent size is usually smaller, it may be
larger due to holes in ('sparse') files, internal
fragmentation, indirect blocks, and the like
-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
-b, --bytes equivalent to '--apparent-size --block-size=1'
-c, --total produce a grand total
-D, --dereference-args dereference only symlinks that are listed on the
command line
-d, --max-depth=N print the total for a directory (or file, with --all)
only if it is N or fewer levels below the command
line argument; --max-depth=0 is the same as
--summarize
--files0-from=F summarize disk usage of the
NUL-terminated file names specified in file F;
if F is -, then read names from standard input
-H equivalent to --dereference-args (-D)
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
--inodes list inode usage information instead of block usage
-k like --block-size=1K
-L, --dereference dereference all symbolic links
-l, --count-links count sizes many times if hard linked
-m like --block-size=1M
-P, --no-dereference don't follow any symbolic links (this is the default)
-S, --separate-dirs for directories do not include size of subdirectories
--si like -h, but use powers of 1000 not 1024
-s, --summarize display only a total for each argument
-t, --threshold=SIZE exclude entries smaller than SIZE if positive,
or entries greater than SIZE if negative
--time show time of the last modification of any file in the
directory, or any of its subdirectories
--time=WORD show time as WORD instead of modification time:
atime, access, use, ctime or status
--time-style=STYLE show times using STYLE, which can be:
full-iso, long-iso, iso, or +FORMAT;
FORMAT is interpreted like in 'date'
-X, --exclude-from=FILE exclude files that match any pattern in FILE
--exclude=PATTERN exclude files that match PATTERN
-x, --one-file-system skip directories on different file systems
--help display this help and exit
--version output version information and exitDisplay values are in units of the first available SIZE from --block-size,
and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).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).GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'du invocation'
2、其他用法简要介绍
//可以用来查看分区的文件系统
df -T

df命令可以显示目前所有文件系统的可用空间及使用情形,请看下列这个例子:
df -h

参数 -h 表示使用「Human-readable」的输出,也就是在档案系统大小使用 GB、MB 等易读的格式。
上面的命令输出的第一个字段(Filesystem)及最后一个字段(Mounted on)分别是档案系统及其挂入点。我们可以看到 /dev/sda1 这个分割区被挂在根目录下。
接下来的四个字段 Size、Used、Avail、及 Use% 分别是该分割区的容量、已使用的大小、剩下的大小、及使用的百分比。 FreeBSD下,当硬盘容量已满时,您可能会看到已使用的百分比超过 100%,因为 FreeBSD 会留一些空间给 root,让 root 在档案系统满时,还是可以写东西到该档案系统中,以进行管理。
//查看系统中文件的使用情况
df -h
//查看当前目录下各个文件及目录占用空间大小
du -sh *
//方法一:切换到要删除的目录,删除目录下的所有文件
rm -f *
//方法二:删除logs文件夹下的所有文件,而不删除文件夹本身
rm -rf logs/*
//匹配特定目录下的文件或文件夹,然后进行删除
ls *.log | xargs rm -f
3、查看linux文件目录的大小和文件夹包含的文件数
统计总数大小
du -sh xmldb/
du -sm * | sort -n //统计当前目录大小 并安大小 排序
du -sk * | sort -n
du -sk * | grep guojf //看一个人的大小
du -m | cut -d "/" -f 2 //看第二个/ 字符前的文字
查看此文件夹有多少文件 /*/*/* 有多少文件
du xmldb/
du xmldb/*/*/* |wc -l
40752
解释:
wc [-lmw]
参数说明:
-l :多少行
-m:多少字符
-w:多少字
4、查看内存使用情况 free -h命令和示例
$ free --help
Usage:
free [options]Options:
-b, --bytes show output in bytes
-k, --kilo show output in kilobytes
-m, --mega show output in megabytes
-g, --giga show output in gigabytes
--tera show output in terabytes
-h, --human show human-readable output
--si use powers of 1000 not 1024
-l, --lohi show detailed low and high memory statistics
-t, --total show total for RAM + swap
-s N, --seconds N repeat printing every N seconds
-c N, --count N repeat printing N times, then exit
-w, --wide wide output--help display this help and exit
-V, --version output version information and exitFor more details see free(1).
示例
$ free -h
total used free shared buff/cache available
Mem: 15G 6.3G 4.6G 154M 4.6G 8.7G
Swap: 15G 2.0G 14G