我们常常会通过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. 纯css实现同一页面下选择之后更换内容效果

    实现效果为如下:在同一页面下,当我选中输入手机号时,出现手机号输入框,当我选中输入验证码时,出现验证码输入框,当我选中设置密码时,出现密码框 在这里有一个小技巧,就是  1.对下面的输入框设置同样的样 ...

  2. WCF未找到终结点

    配置都配了,仍然找不到,config文件没有重新加载,原因不详,只能重新编译一下,就好了....后续找找原因看看

  3. CS academy Growing Trees【模板】DP求树的直径

    [题意概述] 给出一棵树,树上的边有两个值a和b,你可以在[0,limit]范围内选择一个整数delta,树上的边的权值为a+b*delta,现在问当delta为多少的时候树的直径最小.最小直径是多少 ...

  4. 小白菜OJ 1122 公牛母牛配(最大二分图匹配模板)

    题意: n只公牛和m只母牛,某些公牛和某些母牛互相喜欢.但最后一只公牛只能和一只母牛建立一对一匹配.要使得最后牛群匹配对数最大. 链接: http://caioj.cn/problem.php?id= ...

  5. DEV Express中ImageCollection的使用

    1,        ImageCollection作为窗体组件的一种,位于Components分类下,拖进窗体以后,显示在界面的底部. 2,        注意ImageCollection的Imag ...

  6. 【06】sass编译工具(弃)

    [06]编译工具(弃) SASS转译工具 除了使用sass命令来转译SASS文件之外,还可以借助第三方工具完成,目前世面上较为流行的转译工具主要有: Compass.app Scout Codekit ...

  7. 【Codeforces 1083A】The Fair Nut and the Best Path

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们最后要的是一条最长的路径. 这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大. 显然如果我们在某一条边上的累计的权值和< ...

  8. Triangular Pastures (二维01背包)

    描述Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectang ...

  9. POJ3037 Skiing

    Skiing 题目大意: 给定一个M*N的网格,已知在每个网格中的点可以向上下左右四个方向移动一个单位,每个点都有一个高度值. 从每个点开始移动时存在一个速度值,从A点移动到B点,则此时B点的速度为& ...

  10. 从Excel中读取数据(python-xlrd)

    从Excel中读取数据(python-xlrd) 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls ...