delete log file
#!/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; be #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"
Query the file name of the first 100 pieces of data
ls -ltr | tail -n 151 | head -n 100 |awk '{print $9}'
ctime change time
atime access time
mtime : modification time
You can write scripts directly and delete them regularly every day:
For example: (delete all files starting with 20 in the /data/bak directory, suffixed with *.jar.gz, and change all files before 5 days from now, that is to say, only keep the backup files of the last 5 days)
find /data/bak -name "20*.jar.gz" -type f -mtime +5 -exec rm {} \; > /dev/null 2>&1
/data/bak backup directory (change to your own)
2010*_bak.gz file name and type (change to your own, pay attention*)
-type f means to find files of ordinary type, f means ordinary files.
-mtime +5 Find files by their change time, +5 means the file was changed 5 days ago; if so.
-exec rm {} \; means to execute the rm command, the exec option is followed by the command or script to be executed, followed by a pair of { }, a space and a \, and finally a semicolon.
/dev/null 2>&1 redirects standard error to standard output, and then throws it under /DEV/NULL. In layman's terms, it is to throw all standard output and standard error into the trash; where & means to let the command execute in the background.