linux – 如何删除特定文件夹下的文件和文件夹
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux – 如何删除特定文件夹下的文件和文件夹,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1475字,纯文字阅读大概需要3分钟。
内容图文
![linux – 如何删除特定文件夹下的文件和文件夹](/upload/InfoBanner/zyjiaocheng/957/8f3a93437c36415294c0f5d3ccf9c6ee.jpg)
我们知道,要删除文件,我们可以使用此安全选项
find /path/to/directory/ -mindepth 1 -mtime +5 -delete
如果我们要删除/ path / to / directory / folder下的文件夹和文件,该怎么办?应该是什么语法?
删除选项无法删除非空文件夹.
find /var/tmp -type d -mindepth 1 -mtime +5 -delete
find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
find: cannot delete `/var/tmp/foreman-ssh-cmd-1b987fef-10ca-4204-bf4b-441f28a3db07': Directory not empty
find: cannot delete `/var/tmp/foreman-ssh-cmd-2687d337-2b60-4f20-b581-a70807c22cb9': Directory not empty
find: cannot delete `/var/tmp/foreman-ssh-cmd-faedbb3a-7756-4c96-8a40-a4a2001b5fb3': Directory not empty
解决方法:
你没有使用任何-type选项,所以find将删除(-delete)所提供条件匹配的所有内容(精确地是-mindepth 1 -mtime 5 here),无论是文件还是目录(如果为空)或其他任何内容.
如果只想删除空目录:
find /path/to/directory/ -mindepth 1 -type d -mtime +5 -delete
需要注意的是,最好在实际删除之前查看要删除的文件,删除-delete:
find /path/to/directory/ -mindepth 1-type d -mtime +5
为了完整起见,如果您只想搜索文件和目录,请在find中插入OR构造:
find /path/to/directory/ -mindepth 1 \( -type f -o -type d \) -mtime +5
如果要删除非空目录,请在-exec操作中使用rm:
find /path/to/directory/ -mindepth 1 \( -type f -o -type d \) -mtime +5 -exec rm -r {} +
如果需要,添加带有rm的-f.
内容总结
以上是互联网集市为您收集整理的linux – 如何删除特定文件夹下的文件和文件夹全部内容,希望文章能够帮你解决linux – 如何删除特定文件夹下的文件和文件夹所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。