淺談Linux下通過find命令進行rm檔案刪除的小技巧_Linux

來源:互聯網
上載者:User

我們經常會通過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天前的資料的確備刪除了。

問題分析:

進行故障重現,在另一台伺服器中通過類比資料單獨運行find命令分析此問題,測試過程如下:

1.類比資料

[root@edbnode1 ~]# dateWed 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 iptablesdrwxr-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-lolrwxrwxrwx. 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-ipv6lrwxrwxrwx. 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-tunnellrwxrwxrwx. 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-ipv6lrwxrwxrwx. 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 ifupifup-eth  ifup-isdn  ifup-post  ifup-sit  init.ipv6-global  network-functions-ipv6ifcfg-eth1 ifdown ifdown-ippp ifdown-post ifdown-sit ifup-aliasesifup-ippp ifup-plip  ifup-ppp   ifup-tunnel  net.hotplugifcfg-eth2 ifdown-bnep ifdown-ipv6 ifdown-ppp  ifdown-tunnel ifup-bnepifup-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檔案刪除的小技巧就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.