每日一文件
要求:安照这样的日期格式(xxxx-xx-xx)每日生成一个文件,如生成的文件为2017-12-20.log,并且把磁盘的使用情况写到这个文件中,提示:date、df
[[email protected] tmp]# date
2018年 12月 26日 星期三 19:29:13 CST
[[email protected] tmp]# date +%Y
2018
[[email protected] tmp]# date +%y
18
[[email protected] tmp]# date +%d
26
[[email protected] tmp]# date +%m
12
[[email protected] tmp]# date +%H
19
[[email protected] tmp]# date +%M
30
[[email protected] tmp]# date +%S
52
[[email protected] tmp]# date +%s
1545823854
[[email protected] tmp]# date +%F
2018-12-26
[[email protected] tmp]# date +%T
19:31:04
[[email protected] tmp]#
昨天日期
[[email protected] tmp]# date -d "-1 day" +%F
2018-12-25
[[email protected] tmp]#
上一小时
[[email protected] tmp]# date -d "-1 hours" +%T
18:34:54
[[email protected] tmp]#
[[email protected] tmp]# vim 1.sh
#!/bin/bash
d=`date +%F`
df -h > $d.log
~
[[email protected] tmp]# sh 1.sh
[[email protected] tmp]# ls
1.sh ansible-ssh-192.168.242.130-22-root ansible-ssh-192.168.242.133-22-root lua_uwpzx3 tmp.SLBPtZ45L9
2018-12-26.log ansible-ssh-192.168.242.131-22-root elasticsearch.4Kw1U8qo Nginx_proxy_tmp
456.log ansible-ssh-192.168.242.132-22-root hsperfdata_root proxy.log
[[email protected] tmp]#
[[email protected] tmp]# cat 2018-12-26.log
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 18G 6.2G 12G 36% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 30M 881M 4% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 497M 167M 331M 34% /boot
tmpfs 182M 0 182M 0% /run/user/0
overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged
shm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm
overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged
overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged
shm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm
shm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm
[[email protected] tmp]#
改进版
d=`date +%F` #获取当前日期
dir=/data/logs/disklog #指定日志文件生成的目录
if [ ! -d $dir ] #判断如果没有目录创建
then
mkdir -p $dir
fi
df -h > $dir/$d.log #将硬盘信息写到日志
find $dir/ -mtime +365 |xargs rm #删除一年之前的文件
统计IP访问量
awk ‘{print $3}‘ access_log.2018122918 |sort -n|uniq -c|sort -n -r|less
[[email protected] tmp]# vim 2.sh
#!/bin/bash
awk ‘{print $3}‘ access_log.2018122918 |sort -n|uniq -c|sort -n -r
[[email protected] tmp]# sh 2.sh