//盗墓者是个丑奴儿,原
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
//这里直接采用:system函数执行命令。应该使用opendir与readdir,自己去检索最大的文件。让其拥有更大的灵活扩展性。不过太麻烦了就采用的命令。
int main(void){
system("pwd");
system("du --max-depth=1 -a | sort -t . -k 1 -rn | sed -n 2p");
return 0;
}
运行结果:
全部显示:
1:想按输入的参数进行灵活统计,则需要将命令化为数组,插入arg[1]组合命令在system执行。
2:排序只能按字节排序,输出只能输出字节。sort命令-n按照数字的大小排序。有单位就会变成:3m与4k,-n会判断出错。
命令解释:du --max-depth=1 -a | sort -t . -k 1 -rn | sed -n 2p
du命令:只输出当前目录下的文件夹大小,文件不参与统计。-a,目录与文件一并都参与du的统计。-a文件也会统计。
--max-depth=1指定统计目录的深度。1统计当前目录下的文件即可。2应该是当前目录+目录中的子目录。
sort对du输出的结果进行排序。-t分割。-k指定分割后的哪一栏。-n依照数字的大小进行排序。-r倒叙排序
196 ./test。使用-t .按点分为196与test两栏。-k 1只排序分割后的第一栏。只排序了数字栏
sed -n 2p。只显示输出的内容的第二行,2代表输出的内容的行数,n则是输出第n行