Linux 删除大量小文件的两种方案 | 运维进阶
ahcoder 2025-01-14 10:28 21 浏览
【摘要】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如何删除大量小文件
相关推荐
- PC也能装MAX OS X
-
MACBOOK向来以其时尚的外观以及易用的OSX操作系统成为了时(zhuang)尚(bi)人士的最爱。但是其动不动就上万元的昂贵价格,也将一批立志时(zhuang)尚(bi)人士的拒之门外。但是最近...
- 一千多元的笔记本能买吗?英特尔11代+大屏幕,豆小谷值得选吗?
-
前言:有很多粉丝都问过本人,一千多元到底能买到什么样的笔记本?在此笔者只想说,这样的资金预算真的太低了!如果想买全新的,那大概率买的就是性能比较拉垮的上网本,比如搭载英特赛扬N系列、J系列处理器的轻薄...
- 首款配备骁龙X Elite处理器的Linux笔记本:采用KDE Plasma桌面环境
-
德国Linux硬件供应商TUXEDOComputers宣布正在开发一款配备高通骁龙XElite处理器(SnapdragonXEliteSoC)的ARM笔记本电脑,内部将该...
- System76推出Gazelle Linux笔记本:配酷睿i9-13900H处理器
-
IT之家3月30日消息,主打Linux硬件的厂商System76于今天发布了新一代Gazelle笔记本电脑,共有15英寸和17英寸两个版本,将于3月30日接受预订,...
- Kubuntu Focus Xe Gen 2笔记本发布,预装Linux系统
-
IT之家3月25日消息,KubuntuFocusXeGen2笔记本于近日发布,这是一款预装Kubuntu22.04LTSGNU/Linux发行版的轻薄本。上一代Kub...
- 这台Linux笔记本已用上英特尔12代酷睿,最高可选i7-1255U、卖1149美元起
-
Linux笔记本可能因为比较小众,一般都是拿Windows笔记本换个系统而来,硬件上也会落后同期Windows笔记本一两代,不过现在专门做Linux电脑的System76,推出了一款名为LemurP...
- 戴尔Inspiron 14 Plus骁龙笔记本迎新补丁,支持启动Linux
-
IT之家4月25日消息,科技媒体phoronix今天(4月25日)发布博文,报道称最新发布的Linux内核补丁,针对骁龙芯片的戴尔Inspiron14Plus笔记本,让其...
- TUXEDO推出InfinityFlex 14二合一Linux笔记本,配i5-1335U
-
IT之家8月12日消息,Linux硬件企业TUXEDO当地时间本月2日推出了InfinityFlex14二合一Linux笔记本。该笔记本搭载2+8核的英特尔酷睿i5-...
- 登月探测器嫦娥使用什么操作系统,是Linux还是其它自主研发?
-
这是不是国家机密啊。事实什么样的不知道,但是从美国的探测器来看,就算不是也是相似的东西。下面我来说说我知道的。龙芯已经随北斗卫星上天了.就算登月探测器嫦娥是用"龙芯+Linux"也不出奇.没必要...
- DNS分离解析实验
-
如果本文对你有帮助,欢迎关注、点赞、收藏、转发给朋友,让我有持续创作的动力目录一、分离解析概述二、实验需求三、实验步骤3.1双网卡服务器配置3.1.1添加两张网卡(内外网)3.1.2对两个网卡进...
- 一个小实验巩固下进程管理
-
先回顾下之前的三篇文章:Linux进程在内核眼中是什么样子的?Linux进程线程是如何创建的?Linux是如何调度进程的?通过这三篇文章的学习我们知道,无论内核进程还是用户进程,都是可以用task...
- VMware Kali无线WIFI密码破解
-
WIFI破解前准备工作一张支持Kali系统监听的无线网卡VMware虚拟机安装好Kali系统(本实验用的是Kali2022版本)Kali系统下载、安装官方网站:https://www.kali.or...
- python多进程编程
-
forkwindows中是没有fork函数的,一开始直接在Windows中测试,直接报错importosimporttimeret=os.fork()ifret==0:...
- 拔电源十台电脑藏后门!德国实验惊曝Windows致命漏洞
-
2025年4月15日,央视突然曝出一个超级大新闻!原来美国国家安全局通过黑龙江,往微软Windows系统里发送加密信息,激活了系统里藏着的后门程序,想破坏哈尔滨亚冬会!这消息一出来,大家才发现,竟然已...
- 深度探索RK3568嵌入式教学平台实战案例:设备驱动开发实验
-
一、产品简介TL3568-PlusTEB人工智能实验箱国产高性能处理器64位4核低功耗2.0GHz超高主频1T超高算力NPU兼容鸿蒙等国产操作系统二、实验目的1、熟悉基本字符设备的驱动程序...
- 一周热门
- 最近发表
-
- PC也能装MAX OS X
- 一千多元的笔记本能买吗?英特尔11代+大屏幕,豆小谷值得选吗?
- 首款配备骁龙X Elite处理器的Linux笔记本:采用KDE Plasma桌面环境
- System76推出Gazelle Linux笔记本:配酷睿i9-13900H处理器
- Kubuntu Focus Xe Gen 2笔记本发布,预装Linux系统
- 这台Linux笔记本已用上英特尔12代酷睿,最高可选i7-1255U、卖1149美元起
- 戴尔Inspiron 14 Plus骁龙笔记本迎新补丁,支持启动Linux
- TUXEDO推出InfinityFlex 14二合一Linux笔记本,配i5-1335U
- 登月探测器嫦娥使用什么操作系统,是Linux还是其它自主研发?
- DNS分离解析实验
- 标签列表
-
- linux 远程 (37)
- u盘 linux (32)
- linux 登录 (34)
- linux 路径 (33)
- linux 文件命令 (35)
- linux 是什么 (35)
- linux 界面 (34)
- 查看文件 linux (35)
- linux 语言 (33)
- linux代码 (32)
- linux 查看命令 (33)
- 关闭linux (34)
- root linux (33)
- 删除文件 linux (35)
- linux 主机 (34)
- linux与 (33)
- linux 函数 (35)
- linux .ssh (35)
- cpu linux (35)
- 查看linux 系统 (32)
- linux 防火墙 (33)
- linux 手机 (32)
- linux 镜像 (34)
- linux mac (32)
- linux ip地址 (34)