如何在站点静态文件下查找某关键字并修改文件名?
前段時(shí)間,因關(guān)于某些政治性的內(nèi)容,公司部分站點(diǎn)需要做處理,而且數(shù)據(jù)庫(kù)只保存近幾年的,無法通過數(shù)據(jù)庫(kù)刪除或修改早些年的靜態(tài)頁面數(shù)據(jù)
站點(diǎn)文件個(gè)數(shù),不會(huì)少于幾百萬,我想大家不會(huì)一個(gè)個(gè)手工查找吧!也許真的有人那樣搞呢,我只能“呵呵”了
用程序思想去解決工作中遇到的問題,很有必要!
站點(diǎn)環(huán)境:linux
可以使用的shell腳本處理,因?yàn)樗麄兪翘焐谝黄鸬?#xff0c;而且處理上很方便,不過一開始有一個(gè)問題木有考慮到,后面再講
上代碼,shell代碼如下:
#!/bin/sh #?author:?凹凸曼 #?email:?jar-c@163.comd=$1 delname=$2 #delname1=$3 if?[?-z?$d?] thenecho?"站點(diǎn)不存在"?exit?0 fi if?[?-z?$delname?] thenecho?"關(guān)鍵字不存在"exit fi log=./search.log find?$ozvdkddzhkzd -type?f?-iname??"*.htm"?-o?-type?f?-iname?"*.html"?-o?-type?f?-iname?"*.shtml"?-o?-type?f?-iname?"*.shtm"|xargs?grep?"$delname"?>>?${log} #|?cut?-d\:?-f1??|?awk?'{system("mv?"$1"?"$1"xxx"?)}' sleep?1while?read?linedo?d=`echo?$line?|?cut?-d\:?-f1`mv?"$d"?"$d"xxx?|?echo?"$d?修改為?$d"x?>>result.logsleep?1 done?<?${log}mv?${log}?${log}`date?+%Y%m%d%H%s`本地測(cè)試了,木問題,上服務(wù)器測(cè)試,找不到相應(yīng)的關(guān)鍵字的頁面
問題:頁面是亂碼,原因:系統(tǒng)編碼造成的,不想改變正在運(yùn)行中的環(huán)境編碼
就用python重新寫了一個(gè)腳本程序進(jìn)行處理如下:
#?-*-?coding:utf-8?-*- #?author:?凹凸曼 #?email?:?jar-c@163.comimport?os import?os.path import?re #版本要求:2.6至3.0以下 class?ReFile():#目錄__rdir="./"__type=['.htm','.html','.shtml','.shtm']#文件名后綴增加?sss__dsuffix='sss'#處理的關(guān)鍵字__dealstr=''__log="./re.log"def?__init__(self,dir,dealstr):self.__rdir=dirself.__dealstr=dealstrdef?getFiles(self):for?parent,dirnames,filenames?in?os.walk(self.__rdir):for?filename?in?filenames:dirfilename=parent+'/'+filenameif??os.path.splitext(filename)[1]?in?self.__type:try:data=open(dirfilename,'r').read().decode("gbk").encode("utf-8")except?Exception,e:data=open(dirfilename,'r').read()mm=re.search(self.__dealstr,data)if?mm:self.saveLog(dirfilename)os.rename(dirfilename,dirfilename+self.__dsuffix)else:continueelse:continuedef?saveLog(self,c):file=open(self.__log,'a')file.write(str(c)+'\n') if?__name__=='__main__':#ReFile#@param1:?站點(diǎn)路徑?如:/data/site#@param2:?關(guān)鍵字?如:凹凸曼博客|apoylrefile=ReFile("/data/site","凹凸曼博客|apoyl")refile.getFiles()總結(jié):系統(tǒng)運(yùn)維中,常常需要把處理問題的思路實(shí)現(xiàn)成具體程序,去處理一些繁瑣的問題
相關(guān)文章推薦:
Linux有問必答:如何在VMware ESXi虛擬機(jī)上設(shè)置靜態(tài)MAC地址
ArchLinux(2013)中的網(wǎng)絡(luò)配置和靜態(tài)IP時(shí)DNS刷新的解決方法
Linux服務(wù)器靜態(tài)IP設(shè)置與Proftpd連接速度優(yōu)化
本文來自:愛好Linux
本文鏈接:http://www.ahlinux.com/mainte/9345.html
轉(zhuǎn)載于:https://blog.51cto.com/9626185/1579760
總結(jié)
以上是生活随笔為你收集整理的如何在站点静态文件下查找某关键字并修改文件名?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql用户创建,及授权
- 下一篇: 【插件开发】—— 13 GEF双击模型事