-bash-4.1$ ./folder-stats-2.sh
Top 5 files:
./index.html 4000 bytes
./CS133/lab1.html 3245 bytes
./CS118/CW1/Ex1.java 2512 bytes
./CS118/CW2/GrandFinale.java 204 bytes
./.bashrc 20 bytes
我正在编写一个bash脚本,我想知道如何在当前目录中运行脚本时找到前5个最大的文件。提前谢谢。
编辑:它的赋值和使用du,locate,find和任何递归命令来实现任务是不允许的
答案 0 :(得分:2)
du -ab |sort -nr|head -6
在上面一行:
du
-a : all files
-b : use byte as unit
sort
-n : sort as number
-r : reversely
head -6 : we take the first 6 lines,
because the 1st line output by `du`
is the total size of your directory..
输出格式如下:
777777 .
77777 ./foo/bar
7777 ./foo/bar/a.big
777 ./foo/bar/b.big
77 ./foo/bar/blah/bigfile
7 ./other/dir/file
答案 1 :(得分:2)
您可以使用此stat
,sort
,awk
组合:
stat -c "%n:%F:%s" * | sort -t: -rnk3,3 | awk -F: '$2=="regular file"{
printf "%25s\t%s bytes\n", $1, $3} NR>5{exit}'
foo.txt 639 bytes
bar.sh 453 bytes
myscript.sh 383 bytes
baz.pl 330 bytes
proc.sql 328 bytes
stat -c "%n:%F:%s"
打印所有文件名,文件类型和文件大小sort -t: -rnk3,3
用于第3列(大小)awk
命令搜索条件为$2=="regular file"
的所有行(仅打印常规文件)并使用printf
打印格式化输出。一旦我们打印了5行,NR>5{exit}
退出awk
进程。答案 2 :(得分:0)
因为这是一个特权,我会让你开始。 你可能想从
开始for x in * .*
然后在循环中检查“$ x”是否是文件,目录或符号链接
.
和..
您要忽略当循环结束时,打印结果。