How to use the find command in Linux to periodically transfer logs

  

I have learned Linux commands, the find command can be used to find directory files. Today, Xiaobian wants to introduce how to use the find command to periodically migrate Linux logs. Interested friends may wish to come to understand.

In the Linux system, use the find command to periodically migrate linux logs.

One, find command format

find pathname --options [-exec -print -ok . . ]

Second, find command parameter

pathname: directory path to find by find command

-print:find command to output matching file to standard output

The -exec:find command executes the shell command given by this parameter for the matching file. The format is: command {} \\;

-ok: similar to the role of exec

three, find command options

-name: find by file name

-perm: Find by file permissions

-user: Find by user of file

-group: Find by user group of file

-type: Find a file of a certain type

b -- Block device file

d -- Directory

c -- Character device file

p -- Pipe File

l -- Symbolic Linked File

f -- Ordinary File

-mtime: Finds Files by File Change Time

-n - - Indicates that the file change time is within n days from now.

+n -- Indicates that the file change time is now n days ago.

IV, log migration script

The find command is used. The mtime parameter migrates the log files from 2 weeks ago to the specified directory.

The code is as follows:

#! /bin/bash

# site: "a href=“http://www.jb51.net”"www.jb51.net/a"

#1.Standard definition

backup_dir=“/backup/log”

keep_days=14

week_num=`date +%W`

flag=`expr $week_num % 2`

#2. Need to migrate directory

test1=“/var/log/nginx/test1”

migrate_dir=($test1)

#3. Migrate backups, execute every two weeks

if [ $flag -eq 1 ];then

for dir in ${clean_dir[*]}

do

if [ -d $dir ]; then

#Build migration directory

if [ ! -d $backup_dir$dir ];then

mkdir -p $backup_dir$dir

fi

#文件迁移

for file in `find $ Dir -type f -mtime +$keep_days -exec ls {} \\;`

do

mv $file $backup_dir$dir

done

Fi

done

fi

Crontab executes log migration script every two weeks

The code is as follows:

#log periodic migration script< Br>

0 4 * * 7/2 /home/wangzhengyi/scripts/clean-scripts/migrate.sh

Find by file size

The size parameter of the find command can be found by file. to find the size, size of size options as follows:

For example, look for files larger than 10k in the specified directory, the command:

the above is the use of Linux in the find command log The method of regular transfer, in fact, there are many uses of the find command, clever use can solve a lot of practical problems, have you learned?

Copyright © Windows knowledge All Rights Reserved