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?
The Linux system itself is an open source system, so it can be modified during use.
What are file attributes and what are the attributes of the file? I believe many people are not very
In Linux systems, interrupts can be shared so that multiple devices can simultaneously respond to an
When Linux uses the vi editor, vi is stuck, and this happens frequently. What is the cause of vi car
How to change the jenkins port number on Ubuntu
Linux svn error Cant convert string from how to do?
Linux uses the sed command to add a string method
How to use sed command in Linux
Linux how to debug multi-process program
How to set up a Linux system under a RAID array
Linux beginners need to learn the summary of commands
Fedora 21 upgrade Fedora 22 method
Win10 official version mail application 0x80070032 error solution
New features of IE11 browser in Win8.1
Win7 hidden system partitioning skills
Ingeniously set up win7 to automatically connect to the network
What is the difference between Win7 64-bit and 32-bit
Reinstallation system precautions
How to solve the CPU temperature is too high?
How to solve the problem that Win7 can't be started
Experts to check and deal with computer ARP spoofing methods