Linux下查看文件和文件夹大小

1 Linux下查看文件和文件夹大小

2 删除系统日志等

场景:在sts中执行自动部署时候maven提示No space left on device错误,后来经检查发现是磁盘空间满了,用下面的方法分析发现tomcat下面的logs目录占用了很大的空间,删除多余的日志问题解决!

1 Linux下查看文件和文件夹大小

当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择。

 df 可以查看一级文件夹大小、使用比例、档案系统及其挂入点,但对文件却无能为力。

 du 查询文件或文件夹的磁盘使用空间。

  两者配合使用,非常有效。比如用df查看哪个一级目录过大,然后用df查看文件夹或文件的大小,如此便可迅速确定症结。

    如果当前目录下文件和文件夹很多,使用不带参数du的命令,可以循环列出所有文件和文件夹所使用的空间。这对查看究竟是那个地方过大是不利的,所以得指定深入目录的层数,参数:--max-depth=,这是个极为有用的参数!

如下,注意使用“*”,可以得到文件的使用空间大小.

    提醒:一向命令比linux复杂的FreeBSD,它的du命令指定深入目录的层数却是比linux简化,为 -d。

下面的命令与上面的命令有什么异同??强烈推荐下面命令:

du -h --max-depth=1 /home/work/    仅列出home目录下面所有的一级目录文件大小;

du -h --max-depth=1 /home/work/* 列出home下面所有一级目录的一级目录文件大小。

Linux下查看文件和文件夹大小_第1张图片

$ 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 exit

Display 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:
For complete documentation, run: info coreutils 'df invocation'

2、其他用法简要介绍

//可以用来查看分区的文件系统
df -T

Linux下查看文件和文件夹大小_第2张图片

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

df -h

Linux下查看文件和文件夹大小_第3张图片

参数 -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 log/*

//匹配特定目录下的文件或文件夹,然后进行删除

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:多少字

参考:linux下find命令的使用和总结 

你可能感兴趣的:(linux,find,du,Linux)