计算特定目录下前10个大文件

在给定的目录下,想知道有哪些大文件存在,取前十个,按文件大小排行

(1)、使用awk来实现

#!/bin/bash


path=/root/shell


find $path-type f | awk '{

  var=$0

  "ls -l "var |getline var1

  split(var1,a," ")

  filename=a[9]

  filesize=a[5]

  printf("%s %s\n",filename,filesize)| "sort -k2 -n -r|head -10"

}

'

上面的写法在文件较多的时候可能会引起内存溢出。

改进一下如下:

#!/bin/bash


path=/root/shell


echo >r2.txt


find $path-type f | awk '{

  var=$0

  "ls -l "var |getline var1

  split(var1,a," ")

  filename=a[9]

  filesize=a[5]

  printf("%s %s\n",filename,filesize)

}

'>r2.txt


cat r2.txt|sort -k2 -n -r | head -10


(2)、使用while来实现

#!/bin/bash


path=/root/shell


echo >r1.txt


find $path-type f|while read line

do

  filename=`ls -l $line|awk '{print $9}'`

  filesize=`ls -l $line|awk '{print $5}'`


  echo $filename $filesize>>r1.txt

done


cat r1.txt|sort -k2 -n -r | head -10