简体   繁体   English

linux中如何删除超过n天的文件和目录

[英]How to delete files and directories older than n days in linux

I have a directory named repository which has a number of files and sub directories.我有一个名为 repository 的目录,其中包含许多文件和子目录。 I want to find the files and directories which have not been modified since last 14 days so that I can delete those files and directories.我想找到自过去 14 天以来未修改过的文件和目录,以便我可以删除这些文件和目录。 I have wrote this script but it is giving the directory name only我已经写了这个脚本,但它只给出了目录名

#!/bin/sh

M2_REPO=/var/lib/jenkins/.m2/repository
echo $M2_REPO
OLDFILES=/var/lib/jenkins/.m2/repository/deleted_artifacts.txt
AGE=14 

find "${M2_REPO}" -name '*' -atime +${AGE} -exec dirname {} \; >> ${OLDFILES}

find /path/to/files* -mtime +5 -exec rm {} \\;

Note that there are spaces between rm, {}, and \\; 注意rm,{}和\\之间有空格;

Explanation 说明

The first argument is the path to the files. 第一个参数是文件的路径。 This can be a path, a directory, or a wildcard as in the example above. 如上例所示,它可以是路径,目录或通配符。 I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results. 我建议使用完整路径,并确保在不执行exec的情况下运行命令,以确保获得正确的结果。

The second argument, -mtime, is used to specify the number of days old that the file is. 第二个参数-mtime用于指定文件存在的天数。 If you enter +5, it will find files older than 5 days. 如果输入+5,它将查找5天以上的文件。

The third argument, -exec, allows you to pass in a command such as rm. 第三个参数-exec允许您传递诸如rm之类的命令。 The {} \\; {} \\; at the end is required to end the command. 最后需要结束命令。

This should work on Ubuntu, Suse, Redhat, or pretty much any version of linux. 这应该可以在Ubuntu,Suse,Redhat或几乎任何版本的linux上使用。

You can give the find -delete flag to remove the files with it. 您可以给find -delete标志来删除带有它的文件。 Just be careful to put it in the end of the command so that the time filter is applied first. 只需小心地将其放在命令末尾,以便首先应用时间过滤器。

You can first just list the files that the command finds: 您可以首先只列出命令找到的文件:

find "${M2_REPO}" -depth -mtime +${AGE} -print

The -d flag makes the find do the search depth-first, which is implied by the -delete command. -d标志使查找首先进行深度搜索,这是-delete命令所隐含的。

If you like the results, change the print to delete: 如果您喜欢结果,请更改打印以删除:

find "${M2_REPO}" -mtime +${AGE} -delete

I know this is a very old question but FWIW I solved the problem in two steps, first find and delete files older than N days, then find and delete empty directories.我知道这是一个非常古老的问题,但 FWIW 我分两步解决了这个问题,首先查找并删除早于 N 天的文件,然后查找并删除空目录。 I tried doing both in one step but the delete operation updates the modification time on the file's parent directory, and then the (empty) directory does not match the -mtime criteria any more: Here's the solution with shell variables:我尝试一步完成这两项操作,但删除操作会更新文件父目录的修改时间,然后(空)目录不再符合-mtime条件:这是具有 shell 变量的解决方案:

age=14
dir="/tmp/dirty"
find "$dir" -mtime "+$age" -delete && find "$dir" -type d -empty -delete

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

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