我们常常会通过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天前的数据的确备删除了。

问题分析:

进行故障重现。在还有一台server中通过模拟数据单独执行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仅仅找出所需操作文件夹的第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. 測试操作确认改动为:
    [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文件删除的小技巧的更多相关文章

  1. linux下使用dd命令写入镜像文件到u盘

    1.使用 df -h ,查看一下当前各个磁盘 user@host ~/ $ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 119 ...

  2. linux下 利用 rz 命令上传文件

    1. 如何安装? 1)编译安装  root 账号登陆后,依次执行以下命令: # cd /tmp # wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20 ...

  3. Linux下使用SSH命令行传输文件到远程服务器

    目标:CentOS 7 调整 home分区 扩大 root分区 总体过程: 把/home内容备份,然后将/home文件系统所在的逻辑卷删除,扩大/root文件系统,新建/home ,恢复/home内容 ...

  4. linux下通过sed命令直接修改文件内容

    sed是实现对流的编辑.通常,我们使用sed可以实现内容的编辑后然后保存成另外的一个文件,如果正确的话,才写入到源文件.但是某些时候,我们需要直接修改文件,因为,保存文件到一个文件,然后再覆盖原文件的 ...

  5. linux 一个跟踪文件删除的小技巧

    最近有同事问我说他有个现场环境,经常会丢失业务文件,每天都出现,几百个里面丢失1到两个. 为了解决这个问题,我让他布置audit,具体可以man一下auditctl. 过了一天,他说audit.log ...

  6. Linux下查询进程PS或者杀死进程kill的小技巧

    假设我们要kill掉tomcat: 那么我们首先需要tomcat的进程号pid: ps -aux | grep tomcat 记下tomcat的PID后,执行:kill PID(tomcat) 好了, ...

  7. linux 下用find命令查找文件,rm命令删除文件

    linux 下用find命令查找文件,rm命令删除文件. 删除指定目录下指定文件find 要查找的目录名 -name .svn |xargs rm -rf 删除指定名称的文件或文件夹: find -t ...

  8. Linux下查找包含BOM头的文件和清除BOM头命令 2014-08-16 12:30:50

    Linux下查找包含BOM头的文件和清除BOM头命令 2014-08-16 12:30:50 分类: 系统运维 查找包含BOM头的文件,命令如下: 点击(此处)折叠或打开 grep -r -I -l ...

  9. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

随机推荐

  1. 条款3:尽可能使用const(use const whenever possible)

    1.只要这(某值保持不变)是事实,就应该确实说出来,这样可以获得编译器的协助,确保这条约束不被违反. 2.keyword const 有很多种用法,但都简单易用. 2.1classes 外部修饰glo ...

  2. ubuntu 宝塔安装一条龙服务

    ubuntu 安装 1, wget -O install.sh http://download.bt.cn/install/install-ubuntu.sh && sudo bash ...

  3. Sql按照字段分组,选取其他字段最值所在的行记录

    引言: 为什么会引入这个问题,在程序中遇到这样的问题,在某个数据表中,相同的AID(项目ID)被多次添加到数据表中,所以对应于不同的时间,只想选取添加时间最早的哪一条记录. 参考:红黑联盟 所用到的数 ...

  4. luogu1447 [NOI2010]能量采集

    考虑暴力,答案显然是 \(\sum_{i=1}^n\sum_{j=1}^m(2(\gcd(i,j)-1)+1)=\sum_{i=1}^n\sum_{j=1}^m(2\gcd(i,j)-1)\). 考虑 ...

  5. 一些简单的JavaScript的方法

    <script type="text/javascript"> //js跳转 function UserMouserDown() { confirm('你确定要删除吗? ...

  6. Relocation(状压DP)

    Description Emma and Eric are moving to their new house they bought after returning from their honey ...

  7. C#静态构造函数和非静态构造函数

    // 使用静态构造函数时,需要注意几点 //1. 一个类中,最多只能有一个静态构造函数,不允许静态构造函数的重载: //2. 不能加任何访问修饰符(public/private/internale等) ...

  8. BZOJ2662[BeiJing wc2012]冻结【SPFA】

    “我要成为魔法少女!” “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切,封印于卡片之中„„”        在这个愿望被实现以后的世界里,人们享受着魔法卡片(SpellCard ...

  9. hdu 4091

    #include<stdio.h> #include<math.h> __int64 gcd(__int64 a,__int64 b) {  if(b==0)   return ...

  10. 【CF766D】Mahmoud and a Dictionary(并查集)

    题意:有n个单词,给定m个关系,每个关系要么表示单词a与单词b相同,要么表示单词a与单词b相反. 并且“相同”与“相反”有性质:若a与b相同,b与c相同,则a与c相同(从而单词的相同关系是等价关系): ...