老李分享:《Linux Shell脚本攻略》 要点(七)
1、显示给定文件夹下的文件的磁盘适用情况
[root@localhost program_test]# du -a -h ./
320K ./output.tar
96K ./reslt_yang.txt
4.0K ./curr_dir.md5
4.0K ./sed_data.txt
320K ./output.tar
96K ./reslt_yang.txt
4.0K ./curr_dir.md5
4.0K ./sed_data.txt
2、总计磁盘大小使用统计 -c
[root@localhost program_test]# du -h -c ./
48K ./main
4.0K ./abc
16K ./dd_test
544K ./tar-file
404K ./touch_more/test_unzip
984K ./touch_more
2.9M ./
2.9M total
48K ./main
4.0K ./abc
16K ./dd_test
544K ./tar-file
404K ./touch_more/test_unzip
984K ./touch_more
2.9M ./
2.9M total
//只显示统计 -s
[root@localhost program_test]# du -s -h ./
2.9M ./
2.9M ./
3、按文件大小排序
[root@localhost program_test]# du -ak ./ | sort -nrk 1 | head -n 5
2876 ./
984 ./touch_more
544 ./tar-file
404 ./touch_more/test_unzip
320 ./tar-file/output.tar
2876 ./
984 ./touch_more
544 ./tar-file
404 ./touch_more/test_unzip
320 ./tar-file/output.tar
//只比较文件最大的前三位.
[root@localhost program_test]# find ./ -type f -exec du -k {} ; | sort -nrk 1 | head -n 3
320 ./tar-file/output.tar
320 ./output.tar
212 ./11.txt
320 ./tar-file/output.tar
320 ./output.tar
212 ./11.txt
4、磁盘可用情况 df = disk free
[root@localhost program_test]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 2.5G 15G 16% /
tmpfs 504M 80K 504M 1% /dev/shm
/dev/sda1 291M 33M 244M 12% /boot
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 2.5G 15G 16% /
tmpfs 504M 80K 504M 1% /dev/shm
/dev/sda1 291M 33M 244M 12% /boot
5、获取当前用户的相关信息 who、w
[root@localhost pts]# who
yxy tty1 2015-01-02 22:36 (:0)
yxy pts/1 2015-01-02 22:37 (:0.0)
yxy pts/2 2015-01-02 22:37 (192.168.119.1)
yxy pts/3 2015-01-02 22:37 (192.168.119.1)
[root@localhost pts]# w
23:19:38 up 46 min, 4 users, load average: 0.00, 0.02, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
yxy tty1 2015-01-02 22:36 (:0)
yxy pts/1 2015-01-02 22:37 (:0.0)
yxy pts/2 2015-01-02 22:37 (192.168.119.1)
yxy pts/3 2015-01-02 22:37 (192.168.119.1)
[root@localhost pts]# w
23:19:38 up 46 min, 4 users, load average: 0.00, 0.02, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
6、提供系统登陆日志 last
[root@localhost pts]# last
ycy pts/0 192.168.119.1 Fri Jan 2 23:21 still logged in
ycy pts/0 192.168.119.1 Fri Jan 2 23:21 still logged in
7、获取登陆失败的回话信息 lastb //必须以超级管理员运行
[root@localhost pts]# lastb
centos ssh:notty 192.168.119.128 Fri Jan 2 22:59 - 22:59 (00:00)
centos ssh:notty 192.168.119.128 Fri Jan 2 22:59 - 22:59 (00:00)
centos ssh:notty 192.168.119.128 Fri Jan 2 22:59 - 22:59 (00:00)
centos ssh:notty 192.168.119.128 Fri Jan 2 22:59 - 22:59 (00:00)
centos ssh:notty 192.168.119.128 Fri Jan 2 22:59 - 22:59 (00:00)
centos ssh:notty 192.168.119.128 Fri Jan 2 22:59 - 22:59 (00:00)
8、统计最常用的10个命令
[root@localhost program_test]# cat top10_cmds.sh
#!/bin/bash
printf "COMMAND COUNT "
cat ~/.bash_history | awk '{ list[$1]++ }
END{
for(i in list)
{
printf("%s %d
",i, list[i]); }
}' | sort -nrk 2 | head
//执行结果如下:
[root@localhost program_test]# ./top10_cmds.sh
COMMAND COUNT
vi 88
find 82
[root@localhost 75
echo 72
cat 72
ls 65
ll 28
sh 25
seq 22
./word_freq.sh 21