查找并删除超过 x 天的文件或文件夹 [英] find and delete file or folder older than x days

查看:22
本文介绍了查找并删除超过 x 天的文件或文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除超过 7 天的文件和文件夹,所以我尝试了

I want to delete file and folder older than 7 days so I tried

[17:07:14 root@client01.abc.com:~]# find /tmp/ -mindepth 1 -maxdepth 1 -ctime +7 -exec ls -l {} ;

所以当我运行 find/tmp/-mindepth 1 -maxdepth 1 -ctime +7 -exec ls -l {} ; 它不显示任何目录,但对于 find/tmp/-mindepth 1 -maxdepth 2 -ctime +7 -exec ls -l {} ; 它确实在子目录中显示了几个文件.

So when I run find /tmp/ -mindepth 1 -maxdepth 1 -ctime +7 -exec ls -l {} ; it doesnt show any dir, but for find /tmp/ -mindepth 1 -maxdepth 2 -ctime +7 -exec ls -l {} ; it does show few files in subdir.

删除一个特定目录中超过 7 天的文件/文件夹的正确方法是什么?

Whats is the right way to delete files/folders older than 7 days in one specific dir ?

推荐答案

你可以利用这段代码

find /tmp/* -mtime +7 -exec rm {} ;

说明

第一个参数是文件的路径.这可以是路径、目录或通配符,如上例所示.我建议使用完整路径,并确保在没有 exec rm 的情况下运行命令以确保获得正确的结果.

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.

第二个参数 -mtime 用于指定文件存在的天数.如果输入 +7,它将查找 7 天之前的文件.

The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +7, it will find files older than 7 days.

第三个参数 -exec 允许您传入诸如 rm 之类的命令.这 {} ;最后需要结束命令.

The third argument, -exec, allows you to pass in a command such as rm. The {} ; at the end is required to end the command.

来源:http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/

对于删除文件夹,清空其中的文件夹后,您可以在代码段中rmdir安装rm,如果您只想查看目录,您可以添加

For deleting folders, after emptying inside of them you can rmdirinstad of rm in the piece of code, also if you only want to see directories you can add

-type d

代码如下:

find /tmp/*/* -mtime +7 -type d -exec rmdir {} ;

这篇关于查找并删除超过 x 天的文件或文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆