自动删除超过7天的文件

我是linux上的一个完整的菜鸟,但我开始掌握它了。 我有一个运行FTP服务器的Ubuntu Server 16.04来备份安全video文件。 这些文件将存储在以下文件夹中: /home/securityfolder1/home/securityfolder2/home/securityfolder3等。

请注意,每个securityfolderN都是不同的用户。

因为我不希望我的硬盘驱动器一直都是满的,所以我想每天删除这些文件夹中超过7天的文件。

首先,此命令将在名称以securityuser开头的/home中的任何子目录中查找并删除超过7天的所有文件:

 find '/home/securityuser*' -mtime +6 -type f -delete 

你需要-mtime +6而不是+7因为-mtime计算24小时。 正如man find-atime部分所解释的那样( -mtime以相同的方式工作):

  -atime n File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago. 

因此,要查找7天-mtime +6前修改过的文件,您需要查找超过6天前修改过的文件,因此-mtime +6

下一步是让这个命令每天运行一次。 由于每个securityuserN都是不同的用户(您可能希望重新考虑该设置,这会使一切变得更复杂),因此必须以root身份运行。 所以,编辑/etc/crontab

 sudo nano /etc/crontab 

并添加此行:

 @daily root find '/home/securityuser*' -mtime +6 -type f -delete 

这将每天运行一次find命令并删除文件。

据我所知:

尝试find命令:

 find ./dirc/* -mtime +6 -type f -delete ./dirc/* : is your directory (Path) -mtime +6 : modified more than 6 days ago (therefore, at least 7 days ago) -type f : only files -delete : no surprise. Remove it to test before like rm