Linux cronjob 删除超过 99 天的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11119985/
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
cronjob to remove files older than 99 days
提问by user1469220
I have to make a cronjob to remove files older than 99 days in a particular directory but I'm not sure the file names were made by trustworthy Linux users. I must expect special characters, spaces, slash characters, and others.
我必须做一个 cronjob 来删除特定目录中超过 99 天的文件,但我不确定文件名是由值得信赖的 Linux 用户制作的。我必须期待特殊字符、空格、斜杠字符等。
Here is what I think could work:
这是我认为可行的方法:
find /path/to/files -mtime +99 -exec rm {}\;
But I suspect this will fail if there are special characters or if it finds a file that's read-only, (cron may not be run with superuser privileges). I need it to go on if it meets such files.
但是我怀疑如果有特殊字符或找到只读文件(cron 可能无法以超级用户权限运行),这会失败。如果它遇到这样的文件,我需要它继续。
回答by Miquel
If you run rm
with the -f
option, your file is going to be deleted regardless of whether you have write permission on the file or not (all that matters is the containing folder). So, either you can erase all the files in the folder, or none. Add also -r
if you want to erase subfolders.
如果您rm
使用该-f
选项运行,无论您是否对该文件具有写权限,您的文件都将被删除(重要的是包含文件夹)。因此,您可以删除文件夹中的所有文件,也可以不删除。-r
如果要删除子文件夹,也添加。
And I have to say it: be verycareful! You're playing with fire ;) I suggest you debug with something less harmful likfe the file
command.
我不得不说:要非常小心!你在玩火;) 我建议你用一些危害较小的东西来调试,比如file
命令。
You can test this out by creating a bunch of files like, e.g.:
您可以通过创建一堆文件来测试这一点,例如:
touch {a,b,c,d,e,f}
And setting permissions as desired on each of them
并根据需要设置每个权限
回答by lanzz
You should use -execdir
instead of -exec
. Even better, read the full Security considerations for find
chapter in the findutils
manual.
你应该使用-execdir
而不是-exec
. 更好的是,阅读手册中章节的完整安全注意事项find
findutils
。
回答by David W.
When you use -exec rm {} \;
, you shouldn't have any problems with spaces, tabs, returns, or special charactersbecause find
calls the rm
command directly and passes it the name of each file one at a time.
使用 时-exec rm {} \;
,空格、制表符、回车符或特殊字符不应该有任何问题,因为直接find
调用rm
命令并将每个文件的名称一次传递给它。
Directories won't' be removed with that command because you aren't passing it the -r
parameter, and you probably don't want too. That could end up being a bit dangerous. You might also want to include the -f
parameter to do a force in case you don't have write permission. Run the cron script as root, and you should be fine.
该命令不会删除目录,因为您没有将-r
参数传递给它,而且您可能也不想要。这最终可能会有点危险。-f
如果您没有写权限,您可能还希望包含参数以执行强制操作。以 root 身份运行 cron 脚本,你应该没问题。
The only thing I'd worry about is that you might end up hitting a file that you don't want to remove, but has not been modified in the past 100 days. For example, the password to stop the autodestruct sequence at your work. Chances are that file hasn't been modified in the past 100 days, but once that autodestruct sequence starts, you wouldn't want the one to be blamed because the password was lost.
我唯一担心的是,您最终可能会遇到不想删除但在过去 100 天内未修改的文件。例如,在您的工作中停止自动销毁序列的密码。很有可能文件在过去 100 天内没有被修改,但是一旦自动销毁序列开始,您就不会希望因为密码丢失而受到指责。
Okay, more reasonable might be applications that are used but rarely modified. Maybe someone's resume that hasn't been updated because they are holding a current job, etc.
好吧,更合理的可能是使用但很少修改的应用程序。也许某人的简历没有更新,因为他们正在从事当前的工作,等等。
So, be careful with your assumptions. Just because a file hasn't been modified in 100 days doesn't mean it isn't used. A better criteria (although still questionable) is whether the file has been accessed in the last 100 days. Maybe this as a final command:
所以,小心你的假设。仅仅因为一个文件在 100 天内没有被修改并不意味着它没有被使用。一个更好的标准(尽管仍然有问题)是文件是否在过去 100 天内被访问过。也许这是作为最终命令:
find /path/to/files -atime +99 -type f -exec rm -f {}\;
One more thing...
还有一件事...
Some find
commands have a -delete
parameter which can be used instead of the -exec rm
parameter:
一些find
命令有一个-delete
参数,可以用来代替-exec rm
参数:
find /path/to/files -atime +99 -delete
That will delete both found directories and files.
这将删除找到的目录和文件。
One more small recommendation: For the first week, save the files found in a log file instead of removing them, and then examine the log file. This way, you make sure that you're not deleting something important. Once you're happy thet there's nothing in the log file you don't want to touch, you can remove those files. After a week, and you're satisfied that you're not going to delete anything important, you can revert the find
command to do the delete for you.
还有一个小建议:第一周,将在日志文件中找到的文件保存而不是删除它们,然后检查日志文件。这样,您可以确保不会删除重要的内容。一旦您感到满意,日志文件中没有您不想触及的内容,您就可以删除这些文件。一周后,您对不会删除任何重要内容感到满意,您可以恢复find
命令为您执行删除操作。
回答by MBO
Please, always use rm [opts] -- [files]
, this will save you from errors with files like -rf
wiich would otherwise be parsed as options. When you provide file names, then end all options.
请始终使用rm [opts] -- [files]
,这将使您免于出现像-rf
wiich 这样的文件的错误,否则会被解析为选项。当您提供文件名时,请结束所有选项。