Find Command Format
find pathname --options [-exec -print -ok ..]
Find Command Parameters
Pathname: directory path searched by the find command-print: find command outputs the matching file to the standard output-Exec: Find command to execute the shell command given by this parameter on the matching file. format: Command {}\;-OK: similar to the role of Exec
Find Command Options
-Name: search by file name-Perm: search by File Permission-user: search by file user master-group: search by file user group-type: find a type of file B -- block Device File d -- directory c -- character device file p -- pipeline file l -- symbolic link file f -- common file-mtime: find the file according to the file change time-n -- indicates that the file change time is less than N days + n -- indicates that the file change time is earlier than N days ago.
The log migration script uses the mtime parameter of the find command to migrate the log files two weeks ago to the specified directory. For more information, see
#! /Bin/bash #1. standard definition backup_dir = "/backup/log" keep_days = 14week_num = 'date + % W' flag = 'expr $ week_num % 2' #2. you need to migrate the directory test1 = "/var/log/nginx/test1" migrate_dir = ($ test1) #3. perform the migration backup once every two weeks if [$ flag-EQ 1]; thenfor dir in $ {clean_dir [*]} doif [-d $ dir]; then # Build the migration directory if [! -D $ backup_dir $ dir]; thenmkdir-p $ backup_dir $ dirfi # file migration for file in 'Find $ Dir-type F-mtime + $ keep_days-exec ls {}\; 'domv $ File $ backup_dir $ dirdonefidonefi
Crontab executes the log migration script every two weeks.
# Regular log migration script 0 4 ** 7/2/home/wangzhengyi/scripts/clean-scripts/migrate. Sh