Tips for RM file deletion with the Find command under Linux

Source: Internet
Author: User
Tags aliases



We often use the Find command for bulk operations. Such as: bulk delete old files, batch changes, time-based file statistics, file size based file statistics, and so on. In these operations, because the RM delete operation causes a change in the folder structure, it is assumed that a script is written using find combined with RM operations. Will encounter some trouble, this article through a sample for everyone to introduce.


System environment:

SUSE Linux Enterprise Server 11 or

Red Hat Enterprise Linux


Symptoms of the problem:

Customer site has a self-motivated script, with the following find statement, every day to delete files or folders that have been in a folder for the next 7 days, these folders are in chronological order to generate the PostgreSQL database of the Wal log and its error log pg_log:

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

The execution process. The following error occurs intermittently:

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

Obviously, the above command returned the wrong result, but the customer reflected that the data in the folder 7 days before the script execution was deleted.


Problem Analysis:

To reproduce the failure. This issue is analyzed in a separate server that performs the find command by simulating data alone. Test steps such as the following:

  1. Analog data
    [Email protected] ~]# datewed June 23:08:18 CST 2014[[email protected] ~]# cp-rcp/etc/sysconfig/network-scripts//ent Erprisedb_backup/postgresql/[[email protected] ~]# cp-rcp/etc/init.d/iptables/enterprisedb_backup/postgresql/

    # # above the data that is copied to the destination folder is maintained by the CP-RCP command: Build time, user rights root and other information to simulate an old file and an old folder

    [Email protected] ~]# ll/enterprisedb_backup/postgresql/total 16-rwxr-xr-x. 1 root root 9409 Oct  iptablesdrwxr-xr-x. 2 root root 4096 June  network-scripts

    # # above can see the Iptables file was established in 2012, Network-scripts was established in 2013, are far more than 7 days

    [[email protected] ~]# ll/enterprisedb_backup/postgresql/*-rwxr-xr-x. 1 root root 9409 Oct to 2012/enterprisedb_backup/postgresql/iptables/enterprisedb_backup/postgresql/network-scripts: Total 212-rw-r--r--. 1 root root 159 June Ifcfg-eth0-rw-r--r--. 1 root root 203 June Ifcfg-eth1-rw-r--r--. 1 root root 203 June Ifcfg-eth2-rw-r--r--. 1 root root 254 Jan 9 ifcfg-lolrwxrwxrwx. 1 root root June ifdown. /.. /.. /sbin/ifdown-rwxr-xr-x. 1 root root 627 Jan 9 ifdown-bnep-rwxr-xr-x. 1 root root 5397 Jan 9 ifdown-eth-rwxr-xr-x. 1 root root 781 Jan 9 ifdown-ippp-rwxr-xr-x. 1 root root 4168 Jan 9 ifdown-ipv6lrwxrwxrwx. 1 root root One June ifdown-isdn, Ifdown-ippp-rwxr-xr-x. 1 root root 1481 Jan 9 ifdown-post-rwxr-xr-x. 1 root root 1064 Jan 9 ifdown-ppp-rwxr-xr-x. 1 root root 835 Jan 9 ifdown-routes-rwxr-xr-x. 1 root root 1370 Jan 9 IFDOWN-SIT-RWXR-XR-X. 1 root root 1434 Jan 9 ifdown-tunnellrwxrwxrwx. 1 root root: ifup. /.. /.. /sbin/ifup-rwxr-xr-x. 1 root root 12365 Jan 9 ifup-aliases-rwxr-xr-x. 1 root root 859 Jan 9 ifup-bnep-rwxr-xr-x. 1 root root 10157 Jan 9 ifup-eth-rwxr-xr-x. 1 root root 11971 Jan 9 ifup-ippp-rwxr-xr-x. 1 root root 10401 Jan 9 ifup-ipv6lrwxrwxrwx. 1 root root 9 June ifup-isdn-Ifup-ippp-rwxr-xr-x. 1 root root 727 Jan 9 ifup-plip-rwxr-xr-x. 1 root root 954 Jan 9 ifup-plusb-rwxr-xr-x. 1 root root 2364 Jan 9 ifup-post-rwxr-xr-x. 1 root root 4154 Jan 9 ifup-ppp-rwxr-xr-x. 1 root root 1925 Jan 9 ifup-routes-rwxr-xr-x. 1 root root 3499 Jan 9 ifup-sit-rwxr-xr-x. 1 root root 2488 Jan 9 ifup-tunnel-rwxr-xr-x. 1 root root 3770 Jan 9 ifup-wireless-rwxr-xr-x. 1 root root 4623 Jan 9 init.ipv6-global-rwxr-xr-x. 1 root root 1125 Jan 9 net.hotplug-rw-r--r--. 1Root root 13079 Jan 9 network-functions-rw-r--r--. 1 root root 29853 Jan 9 Network-functions-ipv6

    # # above can see Network-script is not an empty folder, there are files, and the files are already established 7 days ago

  2. Test the Find + RM instructions in a separate run script

    [Email protected] ~]#/bin/find/enterprisedb_backup/postgresql/-mtime +7-exec/bin/rm-rf {} \;/bin/find: '/enterprise Db_backup/postgresql/network-scripts ': No such file or Directory[[email protected] ~]# echo $?1[[email protected] ~]# ls/ Enterprisedb_backup/postgresql/[[email protected] ~]#
    Can see that the find operation did return the wrong result, but view the data backup folder discovery, the Iptables file and the Network-scripts folder have been correctly deleted

  3. Since the data has been deleted correctly, we started to suspect that it was deleted by the Network-scripts folder. Find continues to try to delete other files under this folder. Causes no "No such file or directory" errors. It is therefore necessary to further substantiate this conjecture. Run the Data Environment simulation in step 1th above again. and run the following operations, mainly to convert RM to LS to show the overall operating process:
    [[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 Ifupifup-eth ifup-isdn ifup-post ifup-sit init.ipv6-global network-functions-ipv6ifcfg-eth1 ifdown Ifdown-ipp P ifdown-post ifdown-sit ifup-aliasesifup-ippp ifup-plip ifup-ppp ifup-tunnel net.hotplugifcfg-eth2 ifdown-b NEP Ifdown-ipv6 ifdown-ppp ifdown-tunnel ifup-bnepifup-ipv6 ifup-plusb ifup-routes ifup-wireless network-function s/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
    We can see it through the above operation. The find command not only queries the/enterprisedb_backup/postgresql/folder, but also iterates through all subfolders. So it supports our judgment.

  4. In summary, the basic positioning problem lies


Analysis Diagram:

I don't know how to draw this picture.


How to resolve:

    1. After arranging the idea, can confirm, suppose find only to find the action folder of the 1th level files and folders can solve this problem
    2. Through the great man command we get the following information
      -maxdepth levels    

    3. The test operation confirms that the change is:
      [Email protected] ~]#/bin/find/enterprisedb_backup/postgresql/-mtime +7-maxdepth 1-exec/bin/rm-rf {} \;/bin/find: Warning:you has specified the-maxdepth option after a non-option argument-mtime, but options is not positional (-maxd Epth affects tests specified before it as well as those specified after it).  Specify options before other arguments.
      What this means is that the information-mtime found may have-maxdepth the range. In the find operation, it is recommended that-maxdepth be placed before all other parameters

Resolve result "complete"

Tips for RM file deletion with the Find command under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.