小知识: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如何删除大量小文件的更多相关文章
- Linux 快速删除大量小文件方法
进行以下两步操作即可: 1.第一步:创建空的文件夹: mkdir /tmp/blank 2.第二步:执行以下命令:rsync --delete-before -d /tmp/blank/ /home ...
- Linux中删除特殊名称文件的多种方式
今日分享:我们在肉体的疾病方面花了不少钱,精神的病害方面却没有花什么,现在已经到了时候,我们应该有不平凡的学校.--<瓦尔登湖> 前言 我们都知道,在linux删除一个文件可以使用rm命令 ...
- linux下删除文件名乱码文件
linux下通过rm命令来删除文件,但是如果要删除文件名乱码的文件,就不能直接使用rm命令了,因为压根就无法输出文件名来.不过借助find命令可以实现对其删除.在linux下对于每个文件都一个对应的不 ...
- linux 查找删除找定文件
find . -name "*.lastUpdated" -exec rm -rf {} \; 这个命令是find的基本用法,可以分两部分,find ~/ -name " ...
- linux下删除大量小文件
当目录下文件太多时,用rm删除文件会报错:-bash: /bin/rm: Argument list too long提示文件数目太多.解决的办法是使用如下命令:ls | xargs -n 10 rm ...
- Python小练习:批量删除多个文件夹内的相同文件
应用场景: 下载的多个文件夹是压缩包,解压后每个文件夹都有某个网站的推广链接,想要批量的删除该文件 使用环境:win7,python3.6 代码: 1.直接用for循环 由于os.walk()方法自带 ...
- 初始CSS3小知识【99%人不知道的小技巧】
一.引入样式 1.行内样式表 <h1 style="color: red;font-size: 18px;">10-30</h1> 2.内 ...
- linux 下删除乱码的文件夹
[keke.zhaokk@gw2.mpi2.cm10 /home/keke.zhaokk] $ls -i 85082119 dataMining 85082939 ????֦???-???idޢ??? ...
- Linux上删除大量文件几种方式对比
目录 Linux上删除大量文件几种方式对比 1. rm删除:因为文件数量太多,rm无法删除(报错) 2. find查找删除:-exec 3. find查找删除:xargs 4. find调用-dele ...
- Linux小知识:rm -rf/*会将系统全部删除吗
Linux小知识:rm -rf/*会将系统全部删除吗 本文是学习笔记,视频地址为:https://www.bilibili.com/video/av62839850 执行上面的命令并不会删除所有内容( ...
随机推荐
- 6、SpringBoot-mybatis分页实现pagehelper
系列导航 springBoot项目打jar包 1.springboot工程新建(单模块) 2.springboot创建多模块工程 3.springboot连接数据库 4.SpringBoot连接数据库 ...
- uni-app返回上一级,页面不刷新,bug
uniapp 生命周期(onLoad跟onLoadonShow的区别) 一.uniapp生命周期分两种 : 1.应用生命周期:仅可在App.vue中监听,在其它页面监听无效. 2.页面生命周期:仅在p ...
- Mycat 实现分库分表及读写分离
本文为博主原创,未经允许不得转载: Mycat 官网: http://mycat.org.cn/ MyCat 权威指南 文档:http://www.mycat.org.cn/document/myca ...
- CLion创建自定义代码模板
1.问题 很多时候我们都想要简化代码编写,比如像IDEA那样,写入一个sout即会补全为System.out.println( |inserts cursor here| );的形式 最急切的例子便是 ...
- 【MicroPython] 用 c 添加接口 -- 添加 module
[来源]https://www.eemaker.com/micropython-add-module.html
- http-长连接
1. 短链接 http1.0 -- 1个请求-响应过程会创建且1个新的连接 2. 长连接 http1.1 -- 同域下可以创建1个tcp连接,多个请求在同一个tcp上串行处理请求 http2.0 -- ...
- 百度网盘(百度云)SVIP超级会员共享账号每日更新(2024.01.02)
一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...
- Linux_sqlcmd或者是Cloudquery连接SQLSERVER2012的问题解决
Linux_sqlcmd或者是Cloudquery连接SQLSERVER2012的问题解决 背景 最近想使用shell脚本给SQLServer数据库插入数据,但是发现了报错 同时进行CLoudquer ...
- [转帖]利用Python调用outlook自动发送邮件
↓↓↓欢迎关注我的公众号,在这里有数据相关技术经验的优质原创文章↓↓↓ 使用Python发送邮件有两种方式,一种是使用smtp调用邮箱的smtp服务器,另一种是直接调用程序直接发送邮件.而在outlo ...
- 关于cockpit的学习
关于cockpit的学习 背景 使用node-exporter 可以监控很多资源使用情况 但是这个需要搭建一套prometheus和grafana的工具 并且每个机器都需要安装一套node-expor ...