欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談Linux下通過(guò)find命令進(jìn)行rm文件刪除的小技巧

 更新時(shí)間:2016年12月07日 10:09:51   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇淺談Linux下通過(guò)find命令進(jìn)行rm文件刪除的小技巧。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

我們經(jīng)常會(huì)通過(guò)find命令進(jìn)行批量操作,如:批量刪除舊文件、批量修改、基于時(shí)間的文件統(tǒng)計(jì)、基于文件大小的文件統(tǒng)計(jì)等,在這些操作當(dāng)中,由于rm刪除操作會(huì)導(dǎo)致目錄結(jié)構(gòu)變化,如果要通過(guò)find結(jié)合rm的操作寫(xiě)成腳本,就會(huì)遇到一些麻煩,本文通過(guò)一個(gè)例子為大家進(jìn)行介紹。

系統(tǒng)環(huán)境:

SUSE Linux Enterprise Server 11 或

Red Hat Enterprise Linux

問(wèn)題癥狀:

客戶(hù)現(xiàn)場(chǎng)有一個(gè)自動(dòng)化的腳本,有以下的find語(yǔ)句,每天運(yùn)行以刪除某個(gè)目錄下7天以前的文件或目錄,這些目錄都是按時(shí)間順序生成PostgreSQL數(shù)據(jù)庫(kù)的WAL日志及其錯(cuò)誤日志pg_log:

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

運(yùn)行過(guò)程中,間歇性地出現(xiàn)以下錯(cuò)誤:

[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

顯然,以上命令返回了錯(cuò)誤的結(jié)果,但客戶(hù)反映說(shuō),以上腳本運(yùn)行后目錄下7天前的數(shù)據(jù)的確備刪除了。

問(wèn)題分析:

進(jìn)行故障重現(xiàn),在另一臺(tái)服務(wù)器中通過(guò)模擬數(shù)據(jù)單獨(dú)運(yùn)行find命令分析此問(wèn)題,測(cè)試過(guò)程如下:

1.模擬數(shù)據(jù)

[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/## 以上通過(guò) cp -rcp 命令使得拷貝到目標(biāo)目錄的數(shù)據(jù)保持包括:建立時(shí)間、用戶(hù)權(quán)根等信息,以模擬出一個(gè)舊文件及一個(gè)舊目錄

[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年建立的,都遠(yuǎn)遠(yuǎn)超過(guò)了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不是一個(gè)空的目錄,當(dāng)中還有文件,而且文件也都已經(jīng)是7天前建立的了 

2.測(cè)試單獨(dú)模擬執(zhí)行腳本中的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操作的確返回了錯(cuò)誤的結(jié)果,但查看數(shù)據(jù)備份目錄發(fā)現(xiàn),iptables文件及network-scripts目錄已經(jīng)正確刪除

3.由于數(shù)據(jù)已經(jīng)正確刪除,因此我們開(kāi)始懷疑是由network-scripts目錄刪除后,find繼續(xù)嘗試刪除此目錄下其它文件,導(dǎo)致出不“No such file or directory”的錯(cuò)誤,因此需要于進(jìn)一步證實(shí)此猜想,重新執(zhí)行以上“第1步”中的數(shù)據(jù)環(huán)境模擬,并執(zhí)行以下操作,主要是將rm轉(zhuǎn)換成ls以展現(xiàn)整體運(yùn)行過(guò)程:

[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

通過(guò)以上操作我們可以看到,find命令不單查詢(xún)了/enterprisedb_backup/postgresql/目錄,并且遍歷了所有子目錄,因此支持了我們的推斷

4.綜上所述基本定位問(wèn)題所在

解決方案:

1.整理思路后,可以確認(rèn),如果find只找出所需操作目錄的第1層文件及目錄即可解決此問(wèn)題

2.通過(guò)偉大的 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.測(cè)試操作確認(rèn)修改為:

[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.

這里意思是說(shuō):-mtime找到的信息可能會(huì)操過(guò)-maxdepth的范圍,在find操作中建議-maxdepth放在所有其他參數(shù)的前面

解決結(jié)果【完成】

以上這篇淺談Linux下通過(guò)find命令進(jìn)行rm文件刪除的小技巧就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Linux查找特定程序whereis實(shí)例詳解

    Linux查找特定程序whereis實(shí)例詳解

    在本篇文章里小編給大家整理的是關(guān)于Linux查找特定程序 whereis的詳細(xì)用法實(shí)例,有需要的朋友們可以學(xué)習(xí)下。
    2019-11-11
  • Red Hat Linux,Apache2.0+Weblogic9.2負(fù)載均衡集群安裝配置

    Red Hat Linux,Apache2.0+Weblogic9.2負(fù)載均衡集群安裝配置

    這篇文章主要介紹了Red Hat Linux,Apache2.0+Weblogic9.2負(fù)載均衡集群安裝配置,需要的朋友可以參考下
    2014-12-12
  • linux Vim基本操作方法

    linux Vim基本操作方法

    Ubuntu的Vi也不好用,搞了個(gè)Vim用來(lái)學(xué)習(xí)了,簡(jiǎn)單記錄下它的基本操作。還有一本相關(guān)的電子書(shū)可下載。
    2013-11-11
  • Linux多線程中fork與互斥鎖過(guò)程示例

    Linux多線程中fork與互斥鎖過(guò)程示例

    大家好,本篇文章主要講的是Linux多線程中fork與互斥鎖過(guò)程示例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下哦
    2021-11-11
  • yum安裝本地rpm軟件方案詳解

    yum安裝本地rpm軟件方案詳解

    這篇文章主要介紹了yum安裝本地rpm軟件方案詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • ubuntu開(kāi)啟SSH服務(wù)遠(yuǎn)程登錄操作的實(shí)現(xiàn)

    ubuntu開(kāi)啟SSH服務(wù)遠(yuǎn)程登錄操作的實(shí)現(xiàn)

    這篇文章主要介紹了ubuntu開(kāi)啟SSH服務(wù)遠(yuǎn)程登錄操作的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 一文帶你深入理解Linux中的nohup命令

    一文帶你深入理解Linux中的nohup命令

    nohup是Linux和Unix系統(tǒng)中的一個(gè)命令,其作用是在終端退出時(shí),讓進(jìn)程在后臺(tái)繼續(xù)運(yùn)行。這篇文章主要來(lái)和大家講講它的使用,需要的可以參考一下
    2023-03-03
  • PXE如何實(shí)現(xiàn)自動(dòng)化部署Linux系統(tǒng)介紹

    PXE如何實(shí)現(xiàn)自動(dòng)化部署Linux系統(tǒng)介紹

    大家好,本篇文章主要講的是PXE如何實(shí)現(xiàn)自動(dòng)化部署Linux系統(tǒng)介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Apache服務(wù)器中.htaccess文件的實(shí)用配置示例集錦

    Apache服務(wù)器中.htaccess文件的實(shí)用配置示例集錦

    這篇文章主要介紹了Apache服務(wù)器中.htaccess文件的實(shí)用配置示例集錦,囊括了防盜鏈重定向及強(qiáng)制瀏覽器下載指定的文件類(lèi)型等例子,很黃很暴力,需要的朋友可以參考下
    2016-03-03
  • Linux下安裝pyenv的方法

    Linux下安裝pyenv的方法

    這篇文章主要介紹了Linux下安裝pyenv的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評(píng)論