百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Linux 删除大量小文件的两种方案 | 运维进阶

ahcoder 2025-01-14 10:28 35 浏览

【摘要】Linux如何删除大量小文件?本文介绍了两种方法。

【作者】赵靖宇

环境:

RHEL 6.5 + Oracle 11.2.0.4

需求:

使用df -i巡检发现Inodes使用率过高,需要清理删除文件来解决。如果Inodes满,该目录将不能写,即使df -h查看还有剩余空间。

1.问题现象

Oracle的adump下记录的是sys的登陆审计信息,特点是小碎文件非常多,经常会遇到使用rm -rf *命令删除不了,报错-bash: /bin/rm: Argument list too long。

这是因为通配符*在执行时会替换为具体的文件名,例如rm -rf file1 file2 file3 ...,如果文件数量过多,就容易出现这个错误。

比如在下面的环境中,adump目录下文件已达到114万+,执行rm -rf *命令时就会报这个错误:

[oracle@jystdrac2 adump]$ pwd
/opt/app/oracle/admin/crmdb/adump
[oracle@jystdrac2 adump]$ ls|wc -l
1149787
[oracle@jystdrac2 adump]$ rm -rf *
-bash: /bin/rm: Argument list too long
[oracle@jystdrac2 adump]$ du -sh
4.4G

2.解决方案

清楚了问题现象,解决方案就从除去rm -rf *命令的方式之外,还有哪些方法可用,如果通过网络搜索,可能会找到结合find命令再去执行rm的方式,但其实效率非常差,具体写法这里就不列出了,因为我们通常也不会这样处理。那么如何较为效率的删除大批小文件呢?结合网络的经验,并实测验证,最终总结了两种常见的解决方案,效率上也都尚可。

方案一:巧用rsync的方式达到删除目的

建立一个空文件夹,使用rsync --delete-before -d <空文件夹> <需要清理删除小文件的目录>命令最终达到删除大批小文件的目的。下面演示具体操作:

[oracle@jystdrac2 adump]$ mkdir /data/null
[oracle@jystdrac2 adump]$ ls -l /data/null
total 0
[oracle@jystdrac2 ~]$ nohup rsync --delete-before -d /data/null/ /opt/app/oracle/admin/crmdb/adump/ &

使用man rsync查看rsync命令相关的参数说明如下:

-d, --dirs                  transfer directories without recursing
--delete-before         receiver deletes before transfer (default)

方案二:使用find命令的delete参数

使用find <需要清理删除小文件的目录> -type f -delete命令直接删除大批小文件。

使用man find查看find命令相关的参数说明如下:

       -type c
              File is of type c:
              
              b      block (buffered) special
              
              c      character (unbuffered) special
              
              d      directory
              
              p      named pipe (FIFO)
              
              f      regular file
              
              l      symbolic  link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken.  If you want to
                     search for symbolic links when -L is in effect, use -xtype.
                     
              s      socket
              
              D      door (Solaris)
              
       -delete
              Delete files; true if removal succeeded.  If the removal failed, an error message is issued.  If -delete fails, find’s exit status 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  spec-
              ify -depth in order to avoid later surprises.  Because -delete implies -depth, you cannot usefully use -prune and -delete together.

下面演示具体操作:

[oracle@jystdrac1 adump]$ nohup find /opt/app/oracle/admin/crmdb/adump/ -type f -delete &

可以参考下面的命令来简单监控删除过程中Inodes使用率的变化:

while true; do df -i /; sleep 10; done

比如我这里节点jystdrac1使用的find方法,节点jystdrac2使用的rsync方法,实际观察Inodes释放速度区别并不大:

# 使用的find方法,观察Inodes释放速度:
[oracle@jystdrac1 ~]$ while true; do df -i /; sleep 10; done
Filesystem                        Inodes   IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1519124 287772   85% /
Filesystem                        Inodes   IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1519015 287881   85% /
Filesystem                        Inodes   IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1513880 293016   84% /
Filesystem                        Inodes   IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1511132 295764   84% /
Filesystem                        Inodes   IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1502434 304462   84% /
Filesystem                        Inodes   IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1494583 312313   83% /
Filesystem                        Inodes   IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1489111 317785   83% /
Filesystem                        Inodes   IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1487629 319267   83% /


# 使用的rsync方法,观察Inodes释放速度:
[oracle@jystdrac2 ~]$ while true; do df -i /; sleep 10; done
Filesystem                        Inodes  IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 963029 843867   54% /
Filesystem                        Inodes  IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 955037 851859   53% /
Filesystem                        Inodes  IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 953088 853808   53% /
Filesystem                        Inodes  IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 950523 856373   53% /
Filesystem                        Inodes  IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 948754 858142   53% /
Filesystem                        Inodes  IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 944613 862283   53% /
Filesystem                        Inodes  IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 942619 864277   53% /
Filesystem                        Inodes  IUsed  IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 938510 868386   52% /

既然两种方式差异不算大,那就根据需求或个人习惯选择即可。我自己更倾向于使用方案二,因为这样无需创建空目录,操作上也更直观。

最后再总结下删除大量小文件的方法:

# 方案一:
mkdir <空文件夹>
rsync --delete-before -d <空文件夹> <需要清理删除小文件的目录>
# 方案二:
find <需要清理删除小文件的目录> -type f -delete

相对来说这两种方式都比较效率,但由于整体小文件也是比较多,所以实际可以选择nohup放到后台执行。

原题:Linux如何删除大量小文件

相关推荐

KaOS 2025.05版本发布:全面拥抱Qt6,彻底告别Qt5

KaOSLinux2025.05版本重磅发布:全面拥抱Qt6,开启KDE生态新篇章继2025.03版本发布两个月后,专注于KDE桌面环境、采用XFS文件系统的滚动发行版Li...

基于FIMC接口的CMOS摄像头驱动分析与设计

摘要:目前的嵌入式系统中,USB摄像头使用比较普遍,但其应用会受到传输速度的限制。本文采用一款高速CMOS摄像头,其驱动利用S3C6410内置的FIMC接口技术,采用DMA和ping-pong缓冲...

没错是微软 推出基于Linux的交换机系统

2015-09-2205:59:59作者:郑伟你没看错,为了提升自身Azure云数据中心内网络设备的兼容性及开放性,微软也开始推出基于Linux的网络交换机系统了。这个被称为AzureCloud...

Linus Torvalds 宣布首个 Linux 内核 6.16 候选版本

Linux内核负责人兼创始人LinusTorvalds宣布关闭合并窗口,该窗口用于将主要新功能添加到内核中,并开始发布Linux6.16候选版本,从候选版本1(Linux6.16-r...

Linux内核漏洞将影响Haswell架构服务器

在infoq网站上,GilTene最近报告一个十分重要,但并不为人知Linux内核补丁,特别对采用Haswell架构的Linux系统用户和管理员应该特别关注。报告提醒RedHat发行版的用户(包括...

关于Linux性能调优中网络I/O的一些笔记

写在前面和小伙伴分享一些Linux网络优化的笔记,内容很浅,可以用作入门博文内容结合《Linux性能优化》读书笔记整理涉及内容包括常用的优化工具(mii-tool,ethtool,ifconfig,i...

国产操作系统- Veket Linux(国产操作系统之光银河麒麟阅读理解)

VeketLinux是一个随身的可装在U盘的Linux操作系统。主要面向桌面用户。它的设计重点是提供简单易用且稳定的操作系统,同时保持更新和开发。它具有强大的功能集和广泛的用户基础,可满足...

AlmaLinux 9.6发布:升级工具、初步支持IBM Power虚拟化技术

IT之家5月21日消息,科技媒体linuxiac昨日(5月20日)发布博文,报道称代号为SageMargay的AlmaLinux9.6发行版已上线,距上一版本9.5发...

跟老韩学Linux运维架构师系列,vim与view的基本使用

下面是vim和view的10个实例:用vim打开一个新文件:vimnewfile.txt这个命令将会在vim编辑器中打开一个新文件。在vim中移动光标:使用方向键或h、j、k、l键来移动光标。在v...

malloc底层原理剖析——ptmalloc内存池

malloc底层为什么是内存池malloc大家都用过,其是库函数。我们都知道库函数在不同的操作系统中其实执行的是系统调用,那么malloc在Linux上执行的是哪个系统调用呢?brk()和mmap()...

Zen 6架构首秀Linux,AMD加速下一代处理器布局

IT之家5月15日消息,科技媒体Phoronix昨日(5月14日)发布博文,报道称AMD已经开始为下一代“Zen6”处理器做准备,已为该构架向Linux内核提交了首个补丁,...

为何越来越多企业转向安卓/Linux工业平板电脑?答案在这里

在工业领域,设备的稳定性至关重要,尤其是工业平板电脑,常年运行在高温、粉尘、潮湿等复杂环境下,一旦系统崩溃或者卡顿,可能会影响整个生产流程。那么,为什么越来越多的企业选择安卓/Linux工业平板电脑,...

从3ms到0.8ms:ARM+Linux如何重塑工业控制实时性标杆

在智能制造领域,产线控制系统对实时性的要求越来越高。根据行业调研数据,超过65%的工业现场出现过因系统响应延迟导致的故障停机,平均每次停机造成的直接损失高达2-8万元。传统x86架构搭配Windows...

看Linux如何&quot;挖坑种树&quot;

写在前面,有人看我的Linux文章说技术难度不深,笔者不是不想写深,笔者是觉得Linux难就难在入门,入门之后你就知道如何上网查询你所要要解决的Linux需求。如果你已入门,此文已对你无用,请略过此...

AlmaLinux 9.6 发布,新增功能亮点纷呈!

距离上一版本AlmaLinux9.5发布六个月后,基于5.14内核的AlmaLinux正式宣布其企业级Linux发行版的9.x系列第六个更新——AlmaLinux9.6(Sag...