bash 在 Unix 中使用 shell 脚本删除超过 10 天的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13489398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:59:21  来源:igfitidea点击:

Delete files older than 10 days using shell script in Unix

bashshellunixfind

提问by Steve88

I'm new to shell scripts, can anyone help? I want to delete scripts in a folder from the current date back to 10 days. The scripts looks like:

我是 shell 脚本的新手,有人可以帮忙吗?我想将文件夹中的脚本从当前日期删除回 10 天。脚本如下所示:

2012.11.21.09_33_52.script
2012.11.21.09_33_56.script
2012.11.21.09_33_59.script

The script will run in every 10 day with Crontab, that's why I need the current date.

该脚本将使用 Crontab 每 10 天运行一次,这就是我需要当前日期的原因。

回答by Gilles Quenot

findis the common tool for this kind of task :

find是这种任务的常用工具:

find ./my_dir -mtime +10 -type f -delete

EXPLANATIONS

说明

  • ./my_diryour directory (replace with your own)
  • -mtime +10older than 10 days
  • -type fonly files
  • -deleteno surprise. Remove it to test your findfilter before executing the whole command
  • ./my_dir您的目录(替换为您自己的)
  • -mtime +10超过 10 天
  • -type f只有文件
  • -delete没有惊喜。在执行整个命令之前删除它以测试您的find过滤器

And take care that ./my_direxists to avoid bad surprises !

并注意./my_dir存在以避免糟糕的惊喜!

回答by MarcoZen

Just spicing up the shell script above to delete older files but with logging and calculation of elapsed time

只是添加上面的 shell 脚本来删除旧文件,但记录和计算经过的时间

#!/bin/bash

path="/data/backuplog/"
timestamp=$(date +%Y%m%d_%H%M%S)    
filename=log_$timestamp.txt    
log=$path$filename
days=7

START_TIME=$(date +%s)

find $path -maxdepth 1 -name "*.txt"  -type f -mtime +$days  -print -delete >> $log

echo "Backup:: Script Start -- $(date +%Y%m%d_%H%M)" >> $log


... code for backup ...or any other operation .... >> $log


END_TIME=$(date +%s)

ELAPSED_TIME=$(( $END_TIME - $START_TIME ))


echo "Backup :: Script End -- $(date +%Y%m%d_%H%M)" >> $log
echo "Elapsed Time ::  $(date -d 00:00:$ELAPSED_TIME +%Hh:%Mm:%Ss) "  >> $log

The code adds a few things.

代码添加了一些东西。

  • log files named with a timestamp
  • log folder specified
  • find looks for *.txt files only in the log folder
  • type f ensures you only deletes files
  • maxdepth 1 ensures you dont enter subfolders
  • log files older than 7 days are deleted ( assuming this is for a backup log)
  • notes the start / end time
  • calculates the elapsed time for the backup operation...
  • 以时间戳命名的日志文件
  • 指定的日志文件夹
  • find 仅在日志文件夹中查找 *.txt 文件
  • 键入 f 确保您只删除文件
  • maxdepth 1 确保您不会进入子文件夹
  • 超过 7 天的日志文件被删除(假设这是一个备份日志)
  • 记下开始/结束时间
  • 计算备份操作所用的时间...

Note: to test the code, just use -print instead of -print -delete. But do check your path carefully though.

注意:要测试代码,只需使用 -print 而不是 -print -delete。但是请仔细检查您的路径。

Note: Do ensure your server time is set correctly via date - setup timezone/ntp correctly . Additionally check file times with 'stat filename'

注意:请确保通过正确的日期设置时区/ntp 正确设置您的服务器时间。另外用'stat filename'检查文件时间

Note: mtime can be replaced with mmin for better control as mtime discards all fractions (older than 2 days (+2 days) actually means 3 days ) when it deals with getting the timestamps of files in the context of days

注意:mtime 可以替换为 mmin 以更好地控制,因为当 mtime 在处理以天为单位获取文件的时间戳时会丢弃所有分数(早于 2 天(+2 天)实际上意味着 3 天)

-mtime +$days  --->  -mmin  +$((60*24*$days))

回答by glglgl

If you can afford working via the file data, you can do

如果你能负担得起通过文件数据工作,你可以做

find -mmin +14400 -delete