find结合rm删除时的目录结构变化问题
在日常运维中,经常利用find命令执行批量任务,例如清理过期文件、批量修改权限或按时间/大小统计文件等。然而,当结合rm命令进行删除操作时,由于文件系统的目录结构会实时发生变化,直接编写脚本可能会引发一些意外状况。本文将通过一个具体案例来解析这一现象及其解决方案。
系统环境:
SUSE Linux Enterprise Server 11 或
Red Hat Enterprise Linux
问题症状:
某客户现场部署了一套自动化脚本,每日运行以下find语句,旨在删除指定目录下7天前的文件或目录。这些目录主要用于存储PostgreSQL数据库的WAL日志及pg_log错误日志,且生成顺序与时间相关:
/bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf '{}' \;
在脚本运行期间,间歇性地捕获到如下错误信息:
[root@edb ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf {} \;
/bin/find: `/enterprisedb_backup/postgresql/network-scripts': No such file or directory
[root@edb ~]# echo $?
1
从错误输出来看,命令似乎返回了异常结果。不过,经客户反馈确认,脚本执行后,目录下7天前的数据确实已经被正确删除。
问题分析:
为了复现并深入分析该故障,我们在另一台服务器上模拟数据并单独运行find命令进行测试,具体步骤如下:
1. 模拟数据
[root@edbnode1 ~]# date
Wed Jun 18 23:08:18 CST 2014
[root@edbnode1 ~]# cp -rcp /etc/sysconfig/network-scripts/ /enterprisedb_backup/postgresql/
[root@edbnode1 ~]# cp -rcp /etc/init.d/iptables /enterprisedb_backup/postgresql/## 以上通过 cp -rcp 命令使得拷贝到目标目录的数据保持包括:建立时间、用户权根等信息,以模拟出一个旧文件及一个旧目录
[root@edbnode1 ~]# ll /enterprisedb_backup/postgresql/
total 16
-rwxr-xr-x. 1 root root 9409 Oct 31 2012 iptables
drwxr-xr-x. 2 root root 4096 Jun 18 2013 network-scripts## 以上可以看到iptables文件是2012年建立的,network-scripts是2013年建立的,都远远超过了7天
[root@edbnode1 ~]# ll /enterprisedb_backup/postgresql/*
-rwxr-xr-x. 1 root root 9409 Oct 31 2012 /enterprisedb_backup/postgresql/iptables
/enterprisedb_backup/postgresql/network-scripts:
total 212
-rw-r--r--. 1 root root 159 Jun 18 2013 ifcfg-eth0
-rw-r--r--. 1 root root 203 Jun 18 2013 ifcfg-eth1
-rw-r--r--. 1 root root 203 Jun 18 2013 ifcfg-eth2
-rw-r--r--. 1 root root 254 Jan 9 2013 ifcfg-lo
lrwxrwxrwx. 1 root root 20 Jun 18 2013 ifdown -> ../../../sbin/ifdown
-rwxr-xr-x. 1 root root 627 Jan 9 2013 ifdown-bnep
-rwxr-xr-x. 1 root root 5397 Jan 9 2013 ifdown-eth
-rwxr-xr-x. 1 root root 781 Jan 9 2013 ifdown-ippp
-rwxr-xr-x. 1 root root 4168 Jan 9 2013 ifdown-ipv6
lrwxrwxrwx. 1 root root 11 Jun 18 2013 ifdown-isdn -> ifdown-ippp
-rwxr-xr-x. 1 root root 1481 Jan 9 2013 ifdown-post
-rwxr-xr-x. 1 root root 1064 Jan 9 2013 ifdown-ppp
-rwxr-xr-x. 1 root root 835 Jan 9 2013 ifdown-routes
-rwxr-xr-x. 1 root root 1370 Jan 9 2013 ifdown-sit
-rwxr-xr-x. 1 root root 1434 Jan 9 2013 ifdown-tunnel
lrwxrwxrwx. 1 root root 18 Jun 18 2013 ifup -> ../../../sbin/ifup
-rwxr-xr-x. 1 root root 12365 Jan 9 2013 ifup-aliases
-rwxr-xr-x. 1 root root 859 Jan 9 2013 ifup-bnep
-rwxr-xr-x. 1 root root 10157 Jan 9 2013 ifup-eth
-rwxr-xr-x. 1 root root 11971 Jan 9 2013 ifup-ippp
-rwxr-xr-x. 1 root root 10401 Jan 9 2013 ifup-ipv6
lrwxrwxrwx. 1 root root 9 Jun 18 2013 ifup-isdn -> ifup-ippp
-rwxr-xr-x. 1 root root 727 Jan 9 2013 ifup-plip
-rwxr-xr-x. 1 root root 954 Jan 9 2013 ifup-plusb
-rwxr-xr-x. 1 root root 2364 Jan 9 2013 ifup-post
-rwxr-xr-x. 1 root root 4154 Jan 9 2013 ifup-ppp
-rwxr-xr-x. 1 root root 1925 Jan 9 2013 ifup-routes
-rwxr-xr-x. 1 root root 3499 Jan 9 2013 ifup-sit
-rwxr-xr-x. 1 root root 2488 Jan 9 2013 ifup-tunnel
-rwxr-xr-x. 1 root root 3770 Jan 9 2013 ifup-wireless
-rwxr-xr-x. 1 root root 4623 Jan 9 2013 init.ipv6-global
-rwxr-xr-x. 1 root root 1125 Jan 9 2013 net.hotplug
-rw-r--r--. 1 root root 13079 Jan 9 2013 network-functions
-rw-r--r--. 1 root root 29853 Jan 9 2013 network-functions-ipv6
## 以上可以看到network-script不是一个空的目录,当中还有文件,而且文件也都已经是7天前建立的了
2. 测试单独模拟执行脚本中的find + rm指令
[root@edbnode1 ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf {} \;
/bin/find: `/enterprisedb_backup/postgresql/network-scripts': No such file or directory
[root@edbnode1 ~]# echo $?
1
[root@edbnode1 ~]# ls /enterprisedb_backup/postgresql/
[root@edbnode1 ~]#
观察发现,find操作虽然返回了错误代码,但检查数据备份目录后发现,iptables文件及network-scripts目录实际上已经成功删除。
3. 鉴于数据已正确删除,我们推测可能是由于network-scripts目录被删除后,find命令仍尝试访问该路径下的其他文件,从而触发“No such file or directory”错误。为验证这一猜想,需重新模拟“第1步”的数据环境,并执行以下操作,主要将rm替换为ls以展示完整的遍历过程:
[root@edbnode1 ~]# cp -rcp /etc/sysconfig/network-scripts/ /enterprisedb_backup/postgresql/
[root@edbnode1 ~]# cp -rcp /etc/init.d/iptables /enterprisedb_backup/postgresql/
[root@edbnode1 ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/ls {} \;
ifcfg-eth0 ifcfg-lo ifdown-eth ifdown-isdn ifdown-routes ifup ifup-eth ifup-isdn ifup-post ifup-sit init.ipv6-global network-functions-ipv6
ifcfg-eth1 ifdown ifdown-ippp ifdown-post ifdown-sit ifup-aliases ifup-ippp ifup-plip ifup-ppp ifup-tunnel net.hotplug
ifcfg-eth2 ifdown-bnep ifdown-ipv6 ifdown-ppp ifdown-tunnel ifup-bnep ifup-ipv6 ifup-plusb ifup-routes ifup-wireless network-functions
/enterprisedb_backup/postgresql/network-scripts/ifup-plusb
/enterprisedb_backup/postgresql/network-scripts/ifup-sit
/enterprisedb_backup/postgresql/network-scripts/ifdown-post
/enterprisedb_backup/postgresql/network-scripts/ifcfg-lo
/enterprisedb_backup/postgresql/network-scripts/network-functions
/enterprisedb_backup/postgresql/network-scripts/ifup-bnep
/enterprisedb_backup/postgresql/network-scripts/ifup-ippp
/enterprisedb_backup/postgresql/network-scripts/ifdown-sit
/enterprisedb_backup/postgresql/network-scripts/ifdown-tunnel
/enterprisedb_backup/postgresql/network-scripts/ifup-plip
/enterprisedb_backup/postgresql/network-scripts/ifup-eth
/enterprisedb_backup/postgresql/network-scripts/ifdown-ipv6
/enterprisedb_backup/postgresql/network-scripts/ifdown-ippp
/enterprisedb_backup/postgresql/network-scripts/ifup-aliases
/enterprisedb_backup/postgresql/network-scripts/network-functions-ipv6
/enterprisedb_backup/postgresql/network-scripts/ifup-ipv6
/enterprisedb_backup/postgresql/network-scripts/ifup-post
/enterprisedb_backup/postgresql/network-scripts/ifcfg-eth2
/enterprisedb_backup/postgresql/network-scripts/ifcfg-eth1
/enterprisedb_backup/postgresql/network-scripts/ifdown-ppp
/enterprisedb_backup/postgresql/network-scripts/ifup-isdn
/enterprisedb_backup/postgresql/network-scripts/ifcfg-eth0
/enterprisedb_backup/postgresql/network-scripts/ifdown
/enterprisedb_backup/postgresql/network-scripts/ifup-wireless
/enterprisedb_backup/postgresql/network-scripts/ifup-ppp
/enterprisedb_backup/postgresql/network-scripts/ifdown-eth
/enterprisedb_backup/postgresql/network-scripts/init.ipv6-global
/enterprisedb_backup/postgresql/network-scripts/ifdown-isdn
/enterprisedb_backup/postgresql/network-scripts/ifup-tunnel
/enterprisedb_backup/postgresql/network-scripts/ifdown-routes
/enterprisedb_backup/postgresql/network-scripts/ifdown-bnep
/enterprisedb_backup/postgresql/network-scripts/net.hotplug
/enterprisedb_backup/postgresql/network-scripts/ifup
/enterprisedb_backup/postgresql/network-scripts/ifup-routes
/enterprisedb_backup/postgresql/iptables
通过上述操作输出可见,find命令不仅查询了/enterprisedb_backup/postgresql/根目录,还递归遍历了其所有子目录。这证实了我们的推断:目录结构的动态变化导致了后续遍历失败。
4. 综上所述,基本定位了问题的根源。
解决方案:
1. 梳理思路后确认,如果限制find命令仅查找所需操作目录的第一层文件和目录,即可避免遍历已删除的子目录,从而解决此问题。
2. 查阅man手册获取相关信息:
-maxdepth levels
Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments.
3. 测试并确认修改后的命令为:
[root@edbnode1 ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -maxdepth 1 -exec /bin/rm -rf {} \;
/bin/find: warning: you have specified the -maxdepth option after a non-option argument -mtime, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
此处提示指出:-mtime找到的信息可能会超过-maxdepth的范围。在find操作中,建议将-maxdepth参数放在所有其他参数的前面,以避免位置性参数的警告和潜在的行为偏差。
解决结果【完成】
以上关于Linux下通过find命令进行rm文件删除的小技巧分享完毕,希望能为大家提供参考。