Linux下通过find命令进行rm文件删除的小技巧

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下通过find命令进行rm文件删除的小技巧相关的知识,希望对你有一定的参考价值。

技术分享 技术分享 技术分享


我们常常会通过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 ‘{}‘ \;

执行过程中。间歇性地出现下面错误:

[[email protected] ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf {} \;
/bin/find: `/enterprisedb_backup/postgresql/network-scripts‘: No such file or directory
[[email protected] ~]# echo $?
1

显然,以上命令返回了错误的结果,但客户反映说,以上脚本执行后文件夹下7天前的数据的确备删除了。


问题分析:

进行故障重现。在还有一台server中通过模拟数据单独执行find命令分析此问题。測试步骤例如以下:

  1. 模拟数据
    [[email protected] ~]# date
    Wed Jun 18 23:08:18 CST 2014
    
    [[email protected] ~]# cp -rcp /etc/sysconfig/network-scripts/ /enterprisedb_backup/postgresql/
    [[email protected] ~]# cp -rcp /etc/init.d/iptables /enterprisedb_backup/postgresql/

    ## 以上通过 cp -rcp 命令使得复制到目标文件夹的数据保持包含:建立时间、用户权根等信息,以模拟出一个旧文件及一个旧文件夹

    [[email protected] ~]# 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天

    [[email protected] ~]# 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指令

    [[email protected] ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf {} \;
    /bin/find: `/enterprisedb_backup/postgresql/network-scripts‘: No such file or directory
    [[email protected] ~]# echo $?
    1
    [[email protected] ~]# ls /enterprisedb_backup/postgresql/
    [[email protected] ~]#
    
    能够看到find操作的确返回了错误的结果,但查看数据备份文件夹发现,iptables文件及network-scripts文件夹已经正确删除

  3. 因为数据已经正确删除,因此我们開始怀疑是由network-scripts文件夹删除后。find继续尝试删除此文件夹下其他文件。导致出不“No such file or directory”的错误。因此须要于进一步证实此猜想。又一次运行以上“第1步”中的数据环境模拟。并运行下面操作,主要是将rm转换成ls以展现总体运行过程:
    [[email protected] ~]# cp -rcp /etc/sysconfig/network-scripts/ /enterprisedb_backup/postgresql/
    [[email protected] ~]# cp -rcp /etc/init.d/iptables /enterprisedb_backup/postgresql/
    [[email protected] ~]# /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仅仅找出所需操作文件夹的第1层文件及文件夹就可以解决此问题
  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. 測试操作确认改动为:
    [[email protected] ~]# /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文件删除的小技巧的主要内容,如果未能解决你的问题,请参考以下文章

linux中find与rm实现查找并删除目录或文件

find与rm实现查找并删除目录或文件

linux的一个find命令配合rm删除某天前的文件

linux的一个find命令配合rm删除某天前的文件

Linux中的find和grep命令对查找到的文件如何处理啊

linux下如何删除一个文件?