需求
批量修改替換Linux/Windows系統中指定目錄下的目錄名、文件名、文件內容。說明一下,提升工作效率的需求不一定都需要自己動手編程去實現,可以通過借助工具中已有的功能去實現。比如去掉字符串中前、中、后的空格、替換文件指定字符串等,可以借助編輯器中替換功能快速的實現。
Windows系統
1、Windows系統下替換文件內容中的字符串。比如將文件中的字符串"DTS20180506"替換為"DTS20190606"。
(1)使用Notepad++的文件內容替換功能。操作步驟:打開Notepad++ --> 搜索 --> 在文件中查找(也可以使用快捷鍵CTRL+f),界面和使用示例如下:
同時,在這里再介紹一下Windows下操作文本的幾個主要場景:去掉行首空格、行尾空格、行首行尾全部空格,在行首添加字符串,在行尾添加字符串。通過使用正則表達式+文本處理器(Notepad++、UE等)完成
- 去掉行首空格、行尾空格、行首行尾全部空格。下圖表示替換行首空格。如果是替換行尾空格,查找目標輸入為([ \t]$)。注意勾選查找模式中的正則表達式。
如果是Notepad++編輯器,也可以使用菜單中的功能實現。路徑如下:Notepad++主界面 --> 編輯 --> 空白字符操作。
- 在行首添加字符串。在查找目標中輸入^,替換目標中輸入字符串。如需要在每行記錄前面添加字符串root,示例如下
- 在行尾添加字符串。在查找目標中輸入$,替換目標中輸入字符串。如需要在每行行尾添加字符串end。示例如下
2、Windows下修改指定目錄下的文件名和目錄名。假設指定目錄為D:\dir_temp,其子目錄結構如下:
D:\dir_temp>tree /f 卷 LENOVO 的文件夾 PATH 列表 卷序列號為 D892-96E6 D:. └─DTS20180506_001 DTS20180506_001.txt DTS20180506_002.txt DTS20180506_003.txt
其中1個文件內容如下:
D:\dir_temp\DTS20180506_001>type DTS20180506_002.txt DTS20180506_001_001 DTS20180506_001_001
當前需要將文文件名、目錄名和文件中的內容字符串"DTS20180506"替換為"DTS20190606"。
腳本代碼
#-*- encoding:utf-8 -*-
import sys, os, fileinput #global variable define
work_dir = u"D:\dir_temp" old_str = "DTS20180506" new_str = "DTS20190302"
#end
if not os.path.exists(work_dir) or not os.path.isdir(work_dir): print "Please input correct directory,exit" sys.exit(1) for root, dir, files in os.walk(work_dir,topdown=False): for filename in files: file_path = os.path.join(root,filename) #replace old string to new string in file
repl_total = 0 line_repl_count = 0 for line in fileinput.input(file_path, inplace=1): if line.find(old_str) > -1: new_line = line.replace(old_str,new_str) line_repl_count = new_line.count(new_str) repl_total += line_repl_count print new_line, else: print line, #change file name
new_filename = filename.replace(old_str,new_str) new_filename_path = os.path.join(root,new_filename) if filename != new_filename: os.rename(file_path,new_filename_path) else: print "The file %s not change, %d occurrences were replaced" % (filename, repl_total) continue
if os.path.exists(new_filename_path): print "Change file name %s to %s success,, %d occurrences were replaced" % (filename, new_filename, repl_total) else: print "Change file name %s to %s fail." % (filename, new_filename) #change directory name
for eachDir in dir: dir_path = os.path.join(root,eachDir) new_dir_name = eachDir.replace(old_str,new_str) new_dir_name_path = os.path.join(root,new_dir_name) if eachDir != new_dir_name: os.rename(dir_path, new_dir_name_path) else: print "The directory %s not change." % (eachDir,) continue
if os.path.exists(new_dir_name_path): print "Change directory name %s to %s success." % (eachDir, new_dir_name) else: print "Change directory name %s to %s fail." % (eachDir, new_dir_name)
使用方法
(1)將代碼中的work_dir、old_str、new_str分別替換要需要指定操作的目錄、待替換字符串、目標字符串。執行過程如下:
Change file name DTS20180506_001.txt to DTS20190606_001.txt success,, 7 occurrences were replaced Change file name DTS20180506_002.txt to DTS20190606_002.txt success,, 2 occurrences were replaced Change file name DTS20180506_003.txt to DTS20190606_003.txt success,, 7 occurrences were replaced D:\dir_temp\DTS20190606_001 D:\dir_temp\DTS20180506_001 Change directory name DTS20180506_001 to DTS20190606_001 success. 請按任意鍵繼續. . .
子目錄名和文件名修改成功,結果如下:
d:\dir_temp>tree /f 卷 LENOVO 的文件夾 PATH 列表 卷序列號為 D892-96E6 D:. └─DTS20190606_001 DTS20190606_001.txt DTS20190606_002.txt DTS20190606_003.txt
文件內容修改成功,如下:
d:\dir_temp\DTS20190606_001>type DTS20190606_002.txt DTS20190606_001_001 DTS20190606_001_001
Linux系統
1、Linux替換指定目錄下(包含子目錄)的所有文件中的字符串
find . -name "*.txt" -print0 |xargs -n 0 sed -i 's/DTS20180506/DTS20190606/g'
如果僅替換當前目錄下(不包含子目錄)的所有文件中的字符串,可以使用如下命令
sed -i 's/DTS20180506/DTS20190606/g' *.txt
2、Linux中替換指定目錄下的文件名。使用rename命令。下述命令表示將修改當前目錄中以.txt為后綴的文件,將文件名中DTS20180506字符串替換為DTS20190606。
rename DTS20180506 DTS20190606 *.txt
說明:C語言版本的rename指令中文件名可以使用正則表達式匹配,但是新、舊字符串只能精確匹配,這就意味着如果你需要替換的字符串中有一個字符是變化的,就無法進行一次性批量修改。