We often use the Find command for bulk operations, such as: bulk deletion of old files, batch modification, time based file statistics, file size based on document statistics, etc., in these operations, because RM Delete operations can lead to directory structure changes, if you want to use find combined with RM operations written script, Will encounter some trouble, this article through an example for you to introduce.
System environment:
SUSE Linux Enterprise Server 11 or
Red Hat Enterprise Linux
Problem Symptom:
The customer site has an automated script that has the following find statement that runs daily to delete files or directories that were 7 days old in a directory that generated the Wal log of the PostgreSQL database in chronological order and its error log pg_log:
/bin/find/enterprisedb_backup/postgresql/-mtime +7-exec/bin/rm-rf ' {} ';
During the run, the following error occurs intermittently:
[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
Obviously, the above command returns the wrong result, but the customer reflects that the data that was 7 days before the script's running directory was actually deleted.
Problem Analysis:
To perform a failure recurrence, run the Find command separately from the simulation data in another server to analyze the problem, as follows:
1. Analog data
[Root@edbnode1 ~]# date Wed June 23:08:18 CST 2014 [root@edbnode1 ~]#-cp-rcp/etc/sysconfig/network-scripts//ENTERPR isedb_backup/postgresql/[Root@edbnode1 ~]# cp-rcp/etc/init.d/iptables/enterprisedb_backup/postgresql/## above through CP- The RCP command allows data to be copied to the target directory to include: Build time, user right root, etc. to simulate an old file and an old directory [root@edbnode1 ~]# ll/enterprisedb_backup/postgresql/total 16- Rwxr-xr-x. 1 root 9409 Oct iptables drwxr-xr-x. 2 root root 4096 June 2013 network-scripts## above you can see Iptables file was established in 2012, Network-scripts was established in 2013, all far more than 7 days [ Root@edbnode1 ~]# ll/enterprisedb_backup/postgresql/*-rwxr-xr-x.
1 root 9409 Oct 2012/enterprisedb_backup/postgresql/iptables/enterprisedb_backup/postgresql/network-scripts: Total 212-rw-r--r--. 1 Root 159 June 2013 Ifcfg-eth0-rw-r--r--. 1 Root 203 June 2013 Ifcfg-eth1-rw-r--r--. 1 Root 203 June 2013 Ifcfg-eth2-rw-r--r--. 1 root root 254 9 2013 Ifcfg-lo lrwxrwxrwx. 1 root June 2013 Ifdown->. /.. /.. /sbin/ifdoWn-rwxr-xr-x. 1 root root 627 9 2013 ifdown-bnep-rwxr-xr-x. 1 root root 5397 9 2013 ifdown-eth-rwxr-xr-x. 1 root root 781 9 2013 ifdown-ippp-rwxr-xr-x. 1 root root 4168 9 2013 Ifdown-ipv6 lrwxrwxrwx. 1 root June 2013 Ifdown-isdn-> ifdown-ippp-rwxr-xr-x. 1 root root 1481 9 2013 ifdown-post-rwxr-xr-x. 1 root root 1064 9 2013 ifdown-ppp-rwxr-xr-x. 1 root root 835 9 2013 ifdown-routes-rwxr-xr-x. 1 root root 1370 9 2013 ifdown-sit-rwxr-xr-x. 1 root root 1434 9 2013 Ifdown-tunnel lrwxrwxrwx. 1 root June 2013 Ifup->. /.. /.. /sbin/ifup-rwxr-xr-x. 1 root root 12365 9 2013 ifup-aliases-rwxr-xr-x. 1 root root 859 9 2013 ifup-bnep-rwxr-xr-x. 1 root root 10157 9 2013 ifup-eth-rwxr-xr-x. 1 root root 11971 9 2013 ifup-ippp-rwxr-xr-x. 1 root root 10401 9 2013 Ifup-ipv6 lrwxrwxrwx. 1 Root 9 June 2013 Ifup-isdn-> ifup-ippp-rwxr-xr-x. 1 root root 727 9 2013 ifup-plip-rwxr-xr-x. 1 roOT Root 954 9 2013 ifup-plusb-rwxr-xr-x. 1 root root 2364 9 2013 ifup-post-rwxr-xr-x. 1 root root 4154 9 2013 ifup-ppp-rwxr-xr-x. 1 root root 1925 9 2013 ifup-routes-rwxr-xr-x. 1 root root 3499 9 2013 ifup-sit-rwxr-xr-x. 1 root root 2488 9 2013 ifup-tunnel-rwxr-xr-x. 1 root root 3770 9 2013 ifup-wireless-rwxr-xr-x. 1 root root 4623 9 2013 init.ipv6-global-rwxr-xr-x. 1 root 1125 9 2013 Net.hotplug-rw-r--r--. 1 root 13079 9 2013 Network-functions-rw-r--r--. 1 root 29853 9 2013 Network-functions-ipv6 # above you can see Network-script is not an empty directory, there are files, and documents are already 7 days ago established the
2. Test the Find + RM directive in a separate mock execution script
[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 $?< C4/>1
[root@edbnode1 ~]# ls/enterprisedb_backup/postgresql/
[Root@edbnode1 ~]#
You can see that the find operation did return the wrong result, but viewing the data backup directory found that the Iptables file and the network-scripts directory have been properly deleted
3. Since the data has been properly deleted, we are beginning to suspect that after the network-scripts directory was deleted, find continues to attempt to delete the other files in this directory, resulting in no "No such file or directory" error, so it is necessary to further substantiate this conjecture, Re-perform the Data Environment simulation in step 1th above and do the following, primarily by converting RM to LS to show the overall running process:
[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-glob Al Network-functions-ipv6 ifcfg-eth1 ifdown ifdown-ippp ifdown-post ifdown-sit ifup-aliases ifup-ippp ifup-plip ifup-p PP 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
From the above we can see that the find command not only queries the/enterprisedb_backup/postgresql/directory, but also traverses all subdirectories, thus supporting our inference
4. To sum up the basic positioning problem
Solution:
1. After sorting ideas, you can confirm that if find only found the required operation directory of the 1th level of files and directories to resolve this problem
2. Through the great man command we have the following information
-maxdepth levels
descend at most levels (a non-negative integer) levels of directories below the command line argument S.-maxdepth 0 means only apply the tests and actions to the command line arguments.
3. The test operation confirms that the modification is:
[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 positi Onal (-maxdepth affects tests specified before it as OK as those after it). Please specify options before the other arguments.
This means that the information found by-mtime may have passed through the-maxdepth range, and it is recommended that-maxdepth be placed in front of all other parameters in the find operation.
Solution Result "complete"
The above article on the Linux through find command to delete the RM file is small to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.