删除日志

 

删除日志文件

#!/bin/bash
logpath="/data/apache-tomcat-7.0.64/logs"
count=`find  /data/apache-tomcat-7.0.64/logs  -name "*.log" -o -name "*.txt" -type f -mtime +3 | wc -l`
echo  "$count";
if [ "$count" -lt "20" ];then
    echo "file is less 20. no file is removed."
    exit 0;
fi

#find $logpath  -name "*.log" -o -name "*.txt" -type f -mtime +5 -exec rm {} \; > /dev/null 2>&1 ;
find  /data/apache-tomcat-7.0.64/logs  -name "*.log"  -mtime +3 -exec rm {} \; 
find  /data/apache-tomcat-7.0.64/logs  -name "*.txt"  -mtime +3 -exec rm {} \;

count2=` find  /data/apache-tomcat-7.0.64/logs  -name "*.log" -o -name "*.txt" -type f -mtime +3 | wc -l`
echo "$count2"

 

查询出前100条数据的文件名称

ls -ltr | tail -n 151 | head -n 100 |awk '{print $9}'  

ctime更改时间

atime访问时间

mtime :修改时间

可以直接写脚本,每天定时删除:

例如:(删除/data/bak目录下以20开头,后缀为*.jar.gz,更改时间距现在5天以前的所有文件,也就是说只保留最近5天的备份文件)

find /data/bak -name "20*.jar.gz" -type f -mtime +5 -exec rm {} \; > /dev/null 2>&1

/data/bak 备份目录(改成你自己的)

2010*_bak.gz 文件名及类型 (改成你自己的,注意*)

-type f 表示查找普通类型的文件,f表示普通文件。

-mtime +5 按照文件的更改时间来查找文件,+5表示文件更改时间距现在5天以前;如果是 。

-exec rm {} \; 表示执行rm命令,exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。

/dev/null 2>&1 把标准出错重定向到标准输出,然后扔到/DEV/NULL下面去。通俗的说,就是把所有标准输出和标准出错都扔到垃圾桶里面;其中的& 表示让该命令在后台执行。

猜你喜欢

转载自username2.iteye.com/blog/2390762