简体   繁体   English

用于查找、删除和打印已删除文件的 Shell 脚本

[英]Shell script to Find ,Remove and print files removed

I'm creating a script to maintain database backup directories that should retain files only seven days.我正在创建一个脚本来维护应该只保留文件 7 天的数据库备份目录。

This is what i have created这就是我创造的

set +vx
BACKUP_DIR=/mounts/prd
myarray=(`cat /u01/postgres_prod_server.txt`)

for SERVER in "${myarray[@]}"
do
   echo "   SERVER: $SERVER"
   if [ -d $BACKUP_DIR/$SERVER ]; then
      find $BACKUP_DIR/$SERVER/5431/ -type f -name '*.tar' -mtime +7 -exec rm -rf {} \; | tee /tmp/log.txt | wc -l | xargs echo "Files deleted:" >> /tmp/log.txt;
      find $BACKUP_DIR/$SERVER/5432/ -type f -name '*.tar' -mtime +7 -exec rm -rf {} \; | tee /tmp/log1.txt | wc -l | xargs echo "Files deleted:" >> /tmp/log1.txt;
   fi

done

Here is what i'm looking to do but its not working.这是我想要做的,但它不起作用。

1.i'm unable to print what the find command is deleting and send to log file. 1.我无法打印 find 命令正在删除的内容并发送到日志文件。

-exec won't print anything, but you can use -print option to do that. -exec不会打印任何内容,但您可以使用-print选项来执行此操作。 See https://unix.stackexchange.com/a/503530/527050 .请参阅https://unix.stackexchange.com/a/503530/527050

Also, ...| tee /tmp/log.txt还有, ...| tee /tmp/log.txt ...| tee /tmp/log.txt will truncate the log file, so you need to add option -a to keep previous log. ...| tee /tmp/log.txt将截断日志文件,因此您需要添加选项-a以保留以前的日志。

Final script looks like this: (The path of a log file is used twice, so you may want to store it in a variable.)最终脚本如下所示:(日志文件的路径被使用了两次,因此您可能希望将其存储在变量中。)

LOG_FILE=/tmp/log.txt
SEARCH_DIR="$BACKUP_DIR/$SERVER/5431/"

find "${SEARCH_DIR}" -type f -name '*.tar' -mtime +7 \
  -exec rm -rf {} \; -print | \
  tee -a "${LOG_FILE}" | \
  wc -l | \
  xargs echo "Files deleted:" >> "${LOG_FILE}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM