AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 420925
Accepted
DmitrySemenov
DmitrySemenov
Asked: 2012-08-25 09:38:40 +0800 CST2012-08-25 09:38:40 +0800 CST 2012-08-25 09:38:40 +0800 CST

Linux Find 删除文件夹中的文件

  • 772

目标是找到具有给定名称的目录并删除其中的所有文件,保留实际目录

find /home/www/sites/ -iname '_cache' -exec du -hs {} \;

这给了我一个文件列表及其大小

204K    /home/www/sites/test.site.com/html/development/Temporary/_cache
904K    /home/www/sites/test.site2.com/html/development/Temporary/_cache

是否可以用 Linux find 命令实现?

find
  • 4 4 个回答
  • 128 Views

4 个回答

  • Voted
  1. panaroik
    2012-08-25T10:35:37+08:002012-08-25T10:35:37+08:00

    我尝试了一些东西,它似乎正在工作,这是 Alex 在此处发布的类似解决方案。

    find . -iname '_cache' | xargs -I {} find {} -type f -maxdepth 1 -exec rm {} \;
    

    它应该只删除在 _cache 目录中并且只在这个目录中找到的文件。它不会从 _cache 目录的子目录中删除任何文件。

    当然,在使用之前先尝试一下,而不是 rm,放在那里 ls 或其他无害的东西。

    • 3
  2. Alex
    2012-08-25T09:57:52+08:002012-08-25T09:57:52+08:00

    我还没有彻底测试过这个逻辑......但是你可以在循环中做一些事情,比如:

    for findname in $(find /path/to/search -name '_pattern')
    do
      find $findname -type f
    done
    

    因此,您获得了与您的搜索模式匹配的文件列表,然后通过新搜索循环遍历每个文件以查找要删除的文件。

    编写的方式将为您提供一个文件列表,因此您可以将其重定向到一个文件,然后使用 rm 循环遍历该文件。您还可以在 for 循环中将 exec 附加到 find 中。我当然会建议首先按照编写的方式运行以测试逻辑并确保匹配看起来不错。

    • 1
  3. Best Answer
    DmitrySemenov
    2012-08-25T11:09:48+08:002012-08-25T11:09:48+08:00

    正确的命令来擦除它

    find . -iname '_cache' | xargs -I {} find {} -type f -maxdepth 1 -delete 
    
    • 1
  4. 3dinfluence
    2012-08-25T10:24:45+08:002012-08-25T10:24:45+08:00

    find 有一个 -delete 选项。这将删除所有匹配项。

    从手册页

    -删除

              Delete files; true if removal succeeded.  If the removal failed,
              an  error message is issued.  If -delete fails, find's exit stat-
              us will be nonzero (when it eventually exits).  Use of  -delete
              automatically turns on the -depth option.
    
              Warnings:  Don't  forget that the find command line is evaluated
              as an expression, so putting -delete first will make find try to
              delete everything below the starting points you specified.  When
              testing a find command line that you later intend  to  use  with
              -delete,  you should explicitly specify -depth in order to avoid
              later surprises.  Because -delete  implies  -depth,  you  cannot
              usefully use -prune and -delete together.
    
    • 0

相关问题

  • 如何计算目录中的所有子文件夹?

  • 如何查找特定用户可写的所有文件和目录?

  • 如何限制 find 命令搜索仅返回在特定日期之前修改的文件?

  • 忽略查找/输出中的错误

  • 如何在 FreeBSD 中查找文件

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve