1、磁盘统计 df du
[root@stone ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 29G 13G 15G 46% /
/dev/sda3 4.8G 142M 4.4G 4% /home
/dev/sda1 99M 18M 76M 20% /boot
tmpfs 203M 0 203M 0% /dev/shm
none 203M 104K 203M 1% /var/lib/xenstored
#统计磁盘使用情况
#-h表示以易读的方式显示
[root@stone ~]# du linux-2.6.32.60.tar
374268 linux-2.6.32.60.tar
#统计文件大小,默认以字节为单位
[root@stone ~]# du -h linux-2.6.32.60.tar
366M linux-2.6.32.60.tar
#-h表示以易读的方式显示
[root@stone ~]# du -h file1
36K file1
[root@stone ~]# du -h file
12K file/file1
12K file/file2/file1
28K file/file2
2.1M file
#参数为目录时,统计本目录及目录下子目录的大小
[root@stone ~]# du -ah file
1.1M file/f1
1.1M file/f1.iso
4.0K file/file1/f2
12K file/file1
4.0K file/file2/file1/f2
12K file/file2/file1
4.0K file/file2/f4
4.0K file/file2/f3
28K file/file2
2.1M file
[root@stone ~]# du -ah file1
8.0K file1/f1_h
4.0K file1/f2_s
8.0K file1/f2
4.0K file1/config.txt
4.0K file1/hello
36K file1
#-a表示递归输出目录下所有文件的统计结果
[root@stone ~]# du -ch *.txt
4.0K awk.txt
8.0K data.txt
4.0K ls.txt
4.0K printf.txt
8.0K rang_fields.txt
8.0K regular_express.txt
8.0K student_data.txt
44K total
#-c选项会在最后一行显示文件大小总计
[root@stone ~]# du -sh file1
36K file1
[root@stone ~]# du -sh file
2.1M file
[root@stone ~]# du -sh /root/
7.1G /root/
#-s选项只输出一个目录的大小
[root@stone ~]# du -ah file --exclude "*.iso"
1.1M file/f1
4.0K file/file1/f2
12K file/file1
4.0K file/file2/file1/f2
12K file/file2/file1
4.0K file/file2/f4
4.0K file/file2/f3
28K file/file2
1.1M file
[root@stone ~]# du -sh file --exclude "*.iso"
1.1M file
[root@stone ~]# du -ah file1 --exclude "file1/f2*"
8.0K file1/f1_h
4.0K file1/config.txt
4.0K file1/hello
24K file1
#--exclude排除指定的文件
#--exclude-from(-X) file 可以通过file指定需要排除的文件
[root@stone ~]# du -ah file/ --max-depth 1
1.1M file/f1
1.1M file/f1.iso
12K file/file1
28K file/file2
2.1M file/
#--max-depth指定遍历的目录层次的最大深度
#-x指定只能对单个文件系统进行遍历,排除目录下面的挂载点挂载的文件系统
[root@stone ~]# find file/ -maxdepth 1 -type f -exec du -h {} \;
1.1M file/f1
1.1M file/f1.iso
[root@stone ~]# find . -type f -exec du -k {} \; | sort -nrk 1 | head
374268 ./linux-2.6.32.60.tar
10260 ./dd_10mb_file
5136 ./zero
3312 ./ntp-4.2.4p7.tar.gz
2520 ./initrd_2.6.18-164.el5xen
1996 ./.mozilla/firefox/de2nbha6.default/XPC.mfasl
1256 ./awstats-7.1.1-1.noarch.rpm
1240 ./awstats-7.1.1.tar.gz
1032 ./file/f1.iso
1032 ./file/f1
#统计当前目录下最大的10个文件
2、计算命令执行时间
[root@stone ~]# time ls file
f1 f1.iso file1 file2
real 0m0.006s
user 0m0.000s
sys 0m0.008s
[root@stone ~]# /usr/bin/time -o time.txt ls file
f1 f1.iso file1 file2
[root@stone ~]# cat time.txt
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+289minor)pagefaults 0swaps
[root@stone ~]# /usr/bin/time -a -o time.txt ls file1
config.txt f1 f1_h f2 f2_s hello
[root@stone ~]# cat time.txt
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+289minor)pagefaults 0swaps
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+288minor)pagefaults 0swaps
[root@stone ~]# /usr/bin/time -f "Time: %U" -a -o time.txt ls file1
config.txt f1 f1_h f2 f2_s hello
[root@stone ~]# cat time.txt
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+289minor)pagefaults 0swaps
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+288minor)pagefaults 0swaps
Time: 0.00
#-o file 将命令执行时间写入文件file中
#-a表示追加执行时间到文件
#-f表示利用格式字符串格式化时间输出
#%e 表示real时间
#%U 表示user时间
#%S 表示sys时间
3、登陆信息
[root@stone ~]# who
root pts/1 2013-05-23 08:36 (172.16.3.151)
[root@stone ~]# w
11:50:35 up 18:05, 1 user, load average: 0.05, 0.02, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/1 172.16.3.151 08:36 0.00s 0.05s 0.01s w
[root@stone ~]# uptime
11:50:39 up 18:05, 1 user, load average: 0.05, 0.02, 0.00
[root@stone ~]# last | head -5
root pts/2 172.16.3.7 Thu May 23 11:05 - 11:19 (00:14)
root pts/1 172.16.3.151 Thu May 23 08:36 still logged in
root pts/1 172.16.3.151 Wed May 22 17:47 - 18:04 (00:17)
reboot system boot 2.6.18-164.el5xe Wed May 22 17:46 (18:04)
root pts/1 172.16.3.151 Wed May 22 14:11 - down (03:32)
[root@stone ~]# last root| head -5
root pts/2 172.16.3.7 Thu May 23 11:05 - 11:19 (00:14)
root pts/1 172.16.3.151 Thu May 23 08:36 still logged in
root pts/1 172.16.3.151 Wed May 22 17:47 - 18:04 (00:17)
root pts/1 172.16.3.151 Wed May 22 14:11 - down (03:32)
root pts/2 172.16.3.151 Wed May 22 09:15 - 13:06 (03:51)
[root@stone ~]# last reboot | head -5
reboot system boot 2.6.18-164.el5xe Wed May 22 17:46 (18:04)
reboot system boot 2.6.18-164.el5xe Fri May 3 15:19 (19+02:24)
reboot system boot 2.6.18-164.el5xe Thu May 2 09:45 (1+05:32)
reboot system boot 2.6.18-164.el5xe Mon Apr 22 15:39 (10+23:38)
reboot system boot 2.6.18-164.el5xe Thu Apr 18 15:53 (3+23:41)
[root@stone ~]# lastb | head -5
root ssh:notty 172.16.3.151 Wed May 22 09:15 - 09:15 (00:00)
root ssh:notty 172.16.3.151 Fri May 17 08:23 - 08:23 (00:00)
root ssh:notty 172.16.3.151 Sat Apr 27 13:29 - 13:29 (00:00)
shilei ssh:notty stone Thu Apr 25 17:51 - 17:51 (00:00)
shilei ssh:notty stone Thu Apr 25 17:51 - 17:51 (00:00)
#获取失败的用户登陆信息
4、打印出最常使用的10条命令
[root@stone ~]# cat ~/.bash_history | cut -d " " -f1 | sort | uniq -c | sort -rn | head
134 ls
94 echo
77 tar
64 cat
46 rm
37 seq
36 cd
34 ifconfig
30 find
26 tree
[root@stone ~]# cat ~/.bash_history | awk '{list[$1]++;}END{for(i in list){printf("%s\t%d\n",i,list[i]);}}'|sort -nrk 2 | head
ls 144
cat 79
tar 77
du 67
rm 47
echo 41
find 40
cd 36
ifconfig 34
tree 26
5、列出一段时间内占用cpu最多的进程
[root@stone ~]# cat bin/cpu_usage.sh
#!/bin/bash
SECS=120
UNIT_TIME=60
STEPS=$(($SECS/$UNIT_TIME))
echo Watching CPU usage...
for ((i=0;i<STEPS;i++))
do
ps eo comm,pcpu | tail -n +2 >> /tmp/cpu_usage
sleep $UNIT_TIME
done
echo
echo CPU eaters :
cat /tmp/cpu_usage | awk '{process[$1]+=$2;}END{for(i in process){printf("%s\t%d\n",i,process[i])}}' | sort -nrk 2 | head
rm /tmp/cpu_usage
6、用watch监视命令的输出
7、用inotifywait记录文件的访问信息
8、用logrotate管理日志文件
[root@stone ~]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp -- we'll rotate them here
/var/log/wtmp {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
#系统日志配置文件
[root@stone ~]# cat /etc/logrotate.d/httpd
/var/log/httpd/*log {
missingok
notifempty
compress
sharedscripts
postrotate
/sbin/service httpd reload > /dev/null 2>/dev/null || true
endscript
}
#应用日志配置文件
配置文件参数:
参数
|
含义
|
missingok
|
如果日志文件丢失,则忽略,然后返回
|
notifempty
|
仅当源日志文件非空时才对其进行轮替
|
size 100k
|
限制实施轮替的日志文件的大小
|
compress
|
允许用gzip对较旧的日志文件进行压缩
|
weekly
|
指定进行轮替的时间间隔
|
rotate 5 |
指定需要保留的旧日志文件的归档数量 |
create 0600 root root |
指定日志文件的权限,用户及用户组 |
9、用syslog记录日志
[root@stone ~]# logger this is a test log line
[root@stone ~]# tail -5 /var/log/messages
May 24 07:59:12 stone last message repeated 9 times
May 24 08:41:23 stone last message repeated 8 times
May 24 08:42:30 stone last message repeated 12 times
May 24 08:42:37 stone last message repeated 3 times
May 24 08:45:52 stone root: this is a test log line