我们常常会通过find命令进行批量操作。如:批量删除旧文件、批量改动、基于时间的文件统计、基于文件大小的文件统计等。在这些操作其中,因为rm删除操作会导致文件夹结构变化,假设要通过find结合rm的操作写成脚本。就会遇到一些麻烦,本文通过一个样例为大家进行介绍。

系统环境:

SUSE Linux Enterprise Server 11 或

Red Hat Enterprise Linux

问题症状:

客户现场有一个自己主动化的脚本,有下面的find语句,每天执行以删除某个文件夹下7天曾经的文件或文件夹,这些文件夹都是按时间顺序生成PostgreSQL数据库的WAL日志及其错误日志pg_log:

  1. /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf '{}' \;

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

  1. [root@edb ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf {} \;
  2. /bin/find: `/enterprisedb_backup/postgresql/network-scripts': No such file or directory
  3. [root@edb ~]# echo $?
  4. 1

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

问题分析:

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

  1. 模拟数据

    1. [root@edbnode1 ~]# date
    2. Wed Jun 18 23:08:18 CST 2014
    3.  
    4. [root@edbnode1 ~]# cp -rcp /etc/sysconfig/network-scripts/ /enterprisedb_backup/postgresql/
    5. [root@edbnode1 ~]# cp -rcp /etc/init.d/iptables /enterprisedb_backup/postgresql/

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

    1. [root@edbnode1 ~]# ll /enterprisedb_backup/postgresql/
    2. total 16
    3. -rwxr-xr-x. 1 root root 9409 Oct 31 2012 iptables
    4. drwxr-xr-x. 2 root root 4096 Jun 18 2013 network-scripts

    ## 以上能够看到iptables文件是2012年建立的,network-scripts是2013年建立的,都远远超过了7天

    1. [root@edbnode1 ~]# ll /enterprisedb_backup/postgresql/*
    2. -rwxr-xr-x. 1 root root 9409 Oct 31 2012 /enterprisedb_backup/postgresql/iptables
    3.  
    4. /enterprisedb_backup/postgresql/network-scripts:
    5. total 212
    6. -rw-r--r--. 1 root root 159 Jun 18 2013 ifcfg-eth0
    7. -rw-r--r--. 1 root root 203 Jun 18 2013 ifcfg-eth1
    8. -rw-r--r--. 1 root root 203 Jun 18 2013 ifcfg-eth2
    9. -rw-r--r--. 1 root root 254 Jan 9 2013 ifcfg-lo
    10. lrwxrwxrwx. 1 root root 20 Jun 18 2013 ifdown -> ../../../sbin/ifdown
    11. -rwxr-xr-x. 1 root root 627 Jan 9 2013 ifdown-bnep
    12. -rwxr-xr-x. 1 root root 5397 Jan 9 2013 ifdown-eth
    13. -rwxr-xr-x. 1 root root 781 Jan 9 2013 ifdown-ippp
    14. -rwxr-xr-x. 1 root root 4168 Jan 9 2013 ifdown-ipv6
    15. lrwxrwxrwx. 1 root root 11 Jun 18 2013 ifdown-isdn -> ifdown-ippp
    16. -rwxr-xr-x. 1 root root 1481 Jan 9 2013 ifdown-post
    17. -rwxr-xr-x. 1 root root 1064 Jan 9 2013 ifdown-ppp
    18. -rwxr-xr-x. 1 root root 835 Jan 9 2013 ifdown-routes
    19. -rwxr-xr-x. 1 root root 1370 Jan 9 2013 ifdown-sit
    20. -rwxr-xr-x. 1 root root 1434 Jan 9 2013 ifdown-tunnel
    21. lrwxrwxrwx. 1 root root 18 Jun 18 2013 ifup -> ../../../sbin/ifup
    22. -rwxr-xr-x. 1 root root 12365 Jan 9 2013 ifup-aliases
    23. -rwxr-xr-x. 1 root root 859 Jan 9 2013 ifup-bnep
    24. -rwxr-xr-x. 1 root root 10157 Jan 9 2013 ifup-eth
    25. -rwxr-xr-x. 1 root root 11971 Jan 9 2013 ifup-ippp
    26. -rwxr-xr-x. 1 root root 10401 Jan 9 2013 ifup-ipv6
    27. lrwxrwxrwx. 1 root root 9 Jun 18 2013 ifup-isdn -> ifup-ippp
    28. -rwxr-xr-x. 1 root root 727 Jan 9 2013 ifup-plip
    29. -rwxr-xr-x. 1 root root 954 Jan 9 2013 ifup-plusb
    30. -rwxr-xr-x. 1 root root 2364 Jan 9 2013 ifup-post
    31. -rwxr-xr-x. 1 root root 4154 Jan 9 2013 ifup-ppp
    32. -rwxr-xr-x. 1 root root 1925 Jan 9 2013 ifup-routes
    33. -rwxr-xr-x. 1 root root 3499 Jan 9 2013 ifup-sit
    34. -rwxr-xr-x. 1 root root 2488 Jan 9 2013 ifup-tunnel
    35. -rwxr-xr-x. 1 root root 3770 Jan 9 2013 ifup-wireless
    36. -rwxr-xr-x. 1 root root 4623 Jan 9 2013 init.ipv6-global
    37. -rwxr-xr-x. 1 root root 1125 Jan 9 2013 net.hotplug
    38. -rw-r--r--. 1 root root 13079 Jan 9 2013 network-functions
    39. -rw-r--r--. 1 root root 29853 Jan 9 2013 network-functions-ipv6

    ## 以上能够看到network-script不是一个空的文件夹,其中还有文件,并且文件也都已经是7天前建立的了

  2. 測试单独模拟运行脚本中的find + rm指令

    1. [root@edbnode1 ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/rm -rf {} \;
    2. /bin/find: `/enterprisedb_backup/postgresql/network-scripts': No such file or directory
    3. [root@edbnode1 ~]# echo $?
    4. 1
    5. [root@edbnode1 ~]# ls /enterprisedb_backup/postgresql/
    6. [root@edbnode1 ~]#

    能够看到find操作的确返回了错误的结果,但查看数据备份文件夹发现,iptables文件及network-scripts文件夹已经正确删除

  3. 因为数据已经正确删除,因此我们開始怀疑是由network-scripts文件夹删除后。find继续尝试删除此文件夹下其他文件。导致出不“No such file or directory”的错误。因此须要于进一步证实此猜想。又一次运行以上“第1步”中的数据环境模拟。并运行下面操作,主要是将rm转换成ls以展现总体运行过程:
    1. [root@edbnode1 ~]# cp -rcp /etc/sysconfig/network-scripts/ /enterprisedb_backup/postgresql/
    2. [root@edbnode1 ~]# cp -rcp /etc/init.d/iptables /enterprisedb_backup/postgresql/
    3. [root@edbnode1 ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -exec /bin/ls {} \;
    4. 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
    5. ifcfg-eth1 ifdown ifdown-ippp ifdown-post ifdown-sit ifup-aliases ifup-ippp ifup-plip ifup-ppp ifup-tunnel net.hotplug
    6. ifcfg-eth2 ifdown-bnep ifdown-ipv6 ifdown-ppp ifdown-tunnel ifup-bnep ifup-ipv6 ifup-plusb ifup-routes ifup-wireless network-functions
    7. /enterprisedb_backup/postgresql/network-scripts/ifup-plusb
    8. /enterprisedb_backup/postgresql/network-scripts/ifup-sit
    9. /enterprisedb_backup/postgresql/network-scripts/ifdown-post
    10. /enterprisedb_backup/postgresql/network-scripts/ifcfg-lo
    11. /enterprisedb_backup/postgresql/network-scripts/network-functions
    12. /enterprisedb_backup/postgresql/network-scripts/ifup-bnep
    13. /enterprisedb_backup/postgresql/network-scripts/ifup-ippp
    14. /enterprisedb_backup/postgresql/network-scripts/ifdown-sit
    15. /enterprisedb_backup/postgresql/network-scripts/ifdown-tunnel
    16. /enterprisedb_backup/postgresql/network-scripts/ifup-plip
    17. /enterprisedb_backup/postgresql/network-scripts/ifup-eth
    18. /enterprisedb_backup/postgresql/network-scripts/ifdown-ipv6
    19. /enterprisedb_backup/postgresql/network-scripts/ifdown-ippp
    20. /enterprisedb_backup/postgresql/network-scripts/ifup-aliases
    21. /enterprisedb_backup/postgresql/network-scripts/network-functions-ipv6
    22. /enterprisedb_backup/postgresql/network-scripts/ifup-ipv6
    23. /enterprisedb_backup/postgresql/network-scripts/ifup-post
    24. /enterprisedb_backup/postgresql/network-scripts/ifcfg-eth2
    25. /enterprisedb_backup/postgresql/network-scripts/ifcfg-eth1
    26. /enterprisedb_backup/postgresql/network-scripts/ifdown-ppp
    27. /enterprisedb_backup/postgresql/network-scripts/ifup-isdn
    28. /enterprisedb_backup/postgresql/network-scripts/ifcfg-eth0
    29. /enterprisedb_backup/postgresql/network-scripts/ifdown
    30. /enterprisedb_backup/postgresql/network-scripts/ifup-wireless
    31. /enterprisedb_backup/postgresql/network-scripts/ifup-ppp
    32. /enterprisedb_backup/postgresql/network-scripts/ifdown-eth
    33. /enterprisedb_backup/postgresql/network-scripts/init.ipv6-global
    34. /enterprisedb_backup/postgresql/network-scripts/ifdown-isdn
    35. /enterprisedb_backup/postgresql/network-scripts/ifup-tunnel
    36. /enterprisedb_backup/postgresql/network-scripts/ifdown-routes
    37. /enterprisedb_backup/postgresql/network-scripts/ifdown-bnep
    38. /enterprisedb_backup/postgresql/network-scripts/net.hotplug
    39. /enterprisedb_backup/postgresql/network-scripts/ifup
    40. /enterprisedb_backup/postgresql/network-scripts/ifup-routes
    41. /enterprisedb_backup/postgresql/iptables

    通过以上操作我们能够看到。find命令不单查询了/enterprisedb_backup/postgresql/文件夹,而且遍历了全部子文件夹。因此支持了我们的判断

  4. 综上所述基本定位问题所在

分析图解:

这个图不知让咋画

解决方式:

  1. 整理思路后,能够确认,假设find仅仅找出所需操作文件夹的第1层文件及文件夹就可以解决此问题
  2. 通过伟大的 man 命令我们得到下面信息
    1. -maxdepth levels
    2. 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. 測试操作确认改动为:
    1. [root@edbnode1 ~]# /bin/find /enterprisedb_backup/postgresql/ -mtime +7 -maxdepth 1 -exec /bin/rm -rf {} \;
    2. /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. Linux下使用vi命令后退出方式

    退出Vi         当编辑完文件,准备退出Vi返回到shell时,可以使用以下几种方法之一.         在命令模式中,连按两次大写字母Z,若当前编辑的文件曾被修改过,则Vi保存该文件后退出 ...

  2. css选择器(2)——属性选择器和基于元素结构关系的选择器

    在有些标记语言中,不能使用类名和id选择器,于是css2引入了属性选择器. 3.属性选择器 a)根据是否存在该属性来选择 如果希望选择有某个属性的元素,例如要选择有class属性的所有h1元素,使其文 ...

  3. LeetCode01--两数之和

    ''' 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给 ...

  4. 在不是modelAttribute的情况下,如何保存页面输入值的方法(多行遍历)

    <c:forEach var="prdRelInfo" items="${goodRelPrdList}" varStatus="s" ...

  5. python re 正则提取中文

    需求: 提取文本中的中文和数字字母(大小写都要),即相当于删除所有标点符号. 其中new是原字符串 news = re.findall(r'[\u4e00-\u9fa5a-zA-Z0-9]',new)

  6. 什么是TLS?

    最近在Istio实验中经常遇到HTTP,HTTPS,TLS等名词,感觉忘得差不多,需要复习一下计算机网络的知识了. 本文参考   http://www.techug.com/post/https-ss ...

  7. bzoj4568 [Scoi2016]幸运数字 线性基+树链剖分

    A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个 幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征.一些旅行者希望游览 A ...

  8. docker改变镜像源

    sudo echo “DOCKER_OPTS=\”\$DOCKER_OPTS –registry-mirror=http://your-id.m.daocloud.io -d\”” >> ...

  9. 兴奋剂检查(vijos 1426)

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

  10. BZOJ2501: [usaco2010 Oct]Soda Machine

    n<=50000个区间,求哪个点被覆盖区间数量最多,输出这个数量. 差分模板..然而数组忘开两倍.. #include<stdio.h> #include<string.h&g ...