我们常常会通过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. Vue通信、传值的多种方式,详解

    Vue通信.传值的多种方式,详解 转自:https://blog.csdn.net/qq_35430000/article/details/79291287 一.通过路由带参数进行传值 ①两个组件 A ...

  2. set指令详解

    set指令详解 功能说明:设置shell 语 法:set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...] 说 明:设置或取消设置shell选项 ...

  3. CSS---基础外部样式表

    <link rel="stylesheet" type="text/css" href="style.css" media=" ...

  4. svn基本使用详情

    1.增加(Add)先提到变更列表中,再commit到配置库中,选择新增文件,右键SVN菜单执行“Add“操作提交到”变更列表中”,然后右键SVN菜单执行”SVNCommit”提交到版本库中. 2.删除 ...

  5. 比特币 3角对冲python代码

    3角对冲原理 基础货币 base, 兑换货币 quote, 中间货币 mid. 市场分为3个市场 p3: base_quote p2: quote_mid p1: quote_mid 代码逻辑 1, ...

  6. POJ 3169 Layout(差分约束 线性差分约束)

    题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...

  7. oracle自增序列创建

    表atable(id,a) id需要自增 首先建立一个序列:create sequence seq_atable minvalue 1 maxvalue 999999999999999999 star ...

  8. 五、PL/SQL循环、游标、函数和过程

    --PL/SQL基础知识学习 --一.PL/SQL语句块,基础语法格式 DECLARE --变量声明列表 info varchar(25); --变量声明 stu_unm integer := 15; ...

  9. hdu 2063最大匹配

    #include<stdio.h> #include<string.h> int link[600],mark[600],map[600][600],m,n; int find ...

  10. bzoj5108 [CodePlus2017]可做题 位运算dp+离散

    [CodePlus2017]可做题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 87  Solved: 63[Submit][Status][Dis ...