Automatically delete large file logs and expired logs

1. Create a virtual file

1. Use the dd command to simulate creating a file, if: specify the input (input) device or file, of: specify the output (output) device or file, bs: the size of the block (block) transmitted each time, the unit is B (byte ). The default is 512B. The value of bs can specify the unit, such as bs=1b or bs=1k, etc.; count: how many blocks to transmit (the size of the block is specified by bs). If count is not specified, until the input data is exhausted (such as the file encounters EOF), or the output space is full. If it is input from stdin, press ctrl+d to end the input. skip: Skip the previous skip blocks (the size of the block is specified by bs) before transmission. That is to say: the dd command will skip the first skip bs bytes of if, and then transfer the bs count bytes of if to of.

dd if=/dev/zero of=/root/12345/ceshi bs=1M skip=1 count=60

2. Delete the file log exceeding the fixed size

1. du -sh method

#! /bin/bash

DIRPATH=/root/ceshi/

for size in $( du -sh -b $DIRPATH* | awk '{print $1}' )
do
        for file in $(du -sh -b $DIRPATH* | grep ${size} | awk '{print $2}')
        do
                if [ $size -gt 31457280 ] ; then
                        echo "${file} ${size}"
                        rm -rf $file
                fi
        done
done

2. ls -l method

#! /binbash

AIRPATH=/root/ceshi/

for size in $(ls -l $AIRPATH* | awk '{print $5}')
do
          for file in $(ls -l $AIRPATH* | grep ${size} | awk '{print $9}')
          do
                      if [ $size -gt 31457280 ];then
                      echo "${file} ${size}"
                      rm -rf $file
                      fi
          done
done

3. find method

1. According to the size, delete files exceeding the set 1024k

find . -size +1024k | xargs -n 1 rm -rf

4. Du-sh dump and rename method

#! /bin/bash

DIRPATH=/data/nfs-storage/nginx/usr/local/nginx/logs/
AIR=/root/ceshi/bak/
cd $DIRPATH
for size in $( du -sh -b * | awk '{print $1}' )
do
        for file in $(du -sh -b * | grep ${size} | awk '{print $2}')
        do
                if [ $size -gt 21457280 ] ; then
                        echo "${file} ${size}"
                        mkdir -p $AIR
                        echo "${AIR} ${file}"
                        mv $file $AIR$file.`(date +%Y%m%d_%H.%M.%S)`
                fi
        done
done

2. According to the time, if the creation time exceeds the set 10 days or more, it will be deleted

1. find method

Find command parameter details
-name filename #Find the file named filename
-perm #Find by execution permission
-user username #Find by file owner
-group groupname #Find by group
-mtime (mmin/minute) -n + n #Find files by file modification time, -n means within n days, +n means n days ago
-atime (amin/minute) -n +n #Find files by file access time, -n means within n days, +n means n days ago
-ctime (cmin/minute) -n +n #Find files according to the file status change time, including file owner, permission and content modification time, -n means within n days, +n means n days Previously
-nogroup #Check for files without a valid group, that is, the file's group does not exist in /etc/groups -nouser
#Check for files without a valid owner, that is, the file's owner does not exist in /etc/passwd-
type b/d/c/p/l/f #check is a block device, directory, character device, pipe, symbolic link, ordinary file
-size n[c] #check a file with a length of n blocks [or n bytes]
-mount #Do not cross the file system mount point when checking files
-follow #If you encounter a symbolic link file, follow the file pointed to by the link
-prune #Ignore a certain directory

(1) Find and delete files according to the file name suffix

find . -mtime +10 -name "*.bak" -exec rm -rf {} \;

(2) Find and delete files according to the file type
-type f is to specify the file type as a normal file
-type is a parameter of the find command:
-type: find a certain type of document
b: block device document
d: directory
c: character device document
P : pipe documentation
l: symbolic link documentation
f: normal documentation

find . -mtime +1 -type f  -exec rm -rf {} \;

find method shell script

#! /bin/bash

find . -size +1024k | xargs -n 1 rm -rf
find . -ctime +1 -type f  -exec rm -rf {} \;

3. Add scheduled tasks

1. Configure the crontab timing file

crontab -e

2. crontab file configuration content

0 0 * * * find /root/ceshi/* -type f -ctime +2 -exec rm -rf {} \;

0 0 * * * find /root/ceshi/* -size +1024k | xargs -n 1 rm -rf

For detailed crontab configuration, please click this link

おすすめ

転載: blog.csdn.net/weixin_44006354/article/details/126529361