zoukankan      html  css  js  c++  java
  • Linux 查看文件大小并按照大小排序

    使用df 命令查看当前系统磁盘的使用情况:

    [root@node ~]# df  -Th 
    Filesystem              Type      Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root xfs        17G  1.7G   16G  10% /
    devtmpfs                devtmpfs  478M     0  478M   0% /dev
    tmpfs                   tmpfs     489M     0  489M   0% /dev/shm
    tmpfs                   tmpfs     489M  6.7M  482M   2% /run
    tmpfs                   tmpfs     489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1               xfs      1014M  125M  890M  13% /boot
    tmpfs                   tmpfs      98M     0   98M   0% /run/user/0
    

    那如果想查看某个分区下的文件或者目录的大小呢?du命令就可以派上用场了.

    [root@node ~]# du -sh  /*
    0	/bin
    92M	/boot
    0	/dev
    36M	/etc
    0	/home
    0	/lib
    0	/lib64
    0	/media
    0	/mnt
    0	/opt
    du: cannot access ‘/proc/15358/task/15358/fd/4’: No such file or directory
    du: cannot access ‘/proc/15358/task/15358/fdinfo/4’: No such file or directory
    du: cannot access ‘/proc/15358/fd/4’: No such file or directory
    du: cannot access ‘/proc/15358/fdinfo/4’: No such file or directory
    0	/proc
    39M	/root
    6.7M	/run
    0	/sbin
    0	/srv
    0	/sys
    113M	/tmp
    1.4G	/usr
    150M	/var
    

    文件大小:

    [root@node ~]# du -sh ./*
    4.0K	./anaconda-ks.cfg
    39M	./forex-web-4.8.0.172.zip
    [root@node ~]# du -h --max-depth=0 ./*
    4.0K	./anaconda-ks.cfg
    39M	./forex-web-4.8.0.172.zip
    

    可见,上述两个命令的结果是等同的。

    那么要如何将一个目录下所有文件的大小都列出来,并按照文件和文件夹的大小来排序呢?

    我们以/usr 为例

    [root@node ~]# du -sh /usr/*  |sort -nr
    568M	/usr/lib
    280M	/usr/bin
    263M	/usr/share
    160M	/usr/lib64
    57M	/usr/sbin
    36K	/usr/include
    12M	/usr/libexec
    0	/usr/tmp
    0	/usr/src
    0	/usr/local
    0	/usr/games
    0	/usr/etc
    

    通过sort 这个命令来帮助我们排序,但是这个排序不对哦,因为加了-h的参数导致。

    [root@node ~]# du -s /usr/*  |sort -nr
    581208	/usr/lib
    285748	/usr/bin
    268664	/usr/share
    162984	/usr/lib64
    57688	/usr/sbin
    11884	/usr/libexec
    36	/usr/include
    0	/usr/tmp
    0	/usr/src
    0	/usr/local
    0	/usr/games
    0	/usr/etc
    

    这样排序就算正常了。  

    分别选出排在前面的10个和排在后面的10个

    [root@node ~]# du -s /usr/*  |sort -nr | head
    581208	/usr/lib
    285748	/usr/bin
    268664	/usr/share
    162984	/usr/lib64
    57688	/usr/sbin
    11884	/usr/libexec
    36	/usr/include
    0	/usr/tmp
    0	/usr/src
    0	/usr/local
    [root@node ~]# du -s /usr/*  |sort -nr | tail 
    268664	/usr/share
    162984	/usr/lib64
    57688	/usr/sbin
    11884	/usr/libexec
    36	/usr/include
    0	/usr/tmp
    0	/usr/src
    0	/usr/local
    0	/usr/games
    0	/usr/etc 
  • 相关阅读:
    reaver 破解wifi
    CDOJ 1255 斓少摘苹果 图论 2016_5_14
    CDOJ 1256 打表+数组 统计
    poj 3190 贪心+优先队列优化
    poj 2376 Cleaning Shifts 贪心 区间问题
    poj 3253 Fence Repair 贪心
    poj 3069 贪心+区间问题
    poj 3050 Hopscotch DFS+暴力搜索+set容器
    poj 2718 Smallest Difference(暴力搜索+STL+DFS)
    poj 3187 Backward Digit Sums
  • 原文地址:https://www.cnblogs.com/caoshousong/p/10766702.html
Copyright © 2011-2022 走看看