Wednesday, November 5, 2008

Freebsd Hw3_1_2

Requirement:

Print the sum of files' size in current directory(not including subdirectories). Also, find out the largest and the smallest files, and print their filenames, filesizes and owners.


Solution:



#!/bin/sh
ls -alS | awk 'NR==2{max=$0}{sum+=$5}END{printf "%d\n%s\n%s\n" ,sum,max,$0}'
|awk 'NR==1{maxsum=$0}
NR==2{owner=$3;fileName=$9;fileSize=$5}
NR==3{sowner=$3;sfileName=$9;sfileSize=$5}
END
{printf"%d\nLargest file : %s %d owner:%s\nSmallest file: %s %d owner:%s\n",maxsum,fileName,fileSize,owner,sfileName,sfileSize,sowner}'


對目錄執行的話
testpc [~/temp] -testpc-> ls -alS
total 30
-rw-r--r-- 1 testpc testpc 9257 10 29 23:38 tmp1
drwxr-xr-x 4 testpc testpc 512 11 1 13:03 ./
drwxr-xr-x 3 testpc testpc 512 10 29 21:58 ../
drwxr-xr-x 2 testpc testpc 512 10 30 00:23 abc/
drwxr-xr-x 3 testpc testpc 512 10 31 19:38 tmp/
-rwxr-xr-x 1 testpc testpc 380 11 1 13:01 test2.sh*
-rw-r--r-- 1 testpc testpc 329 10 31 21:15 example.sh
-rw-r--r-- 1 testpc testpc 310 11 1 13:58 test1_2.sh
-rwxr-xr-x 1 testpc testpc 121 10 31 20:41 test1_1.sh*
-rw-r--r-- 1 testpc testpc 66 10 31 21:13 err.log
-rw-r--r-- 1 testpc testpc 42 11 1 13:14 change.sh

結果如下
testpc [~/temp] -testpc-> sh test1_2.sh
12571
Largest file : tmp1 9257 owner:testpc
Smallest file: change.sh 42 owner:testpc

0 comments:

Post a Comment