需求
批量修改替换Linux/Windows系统中指定目录下的目录名、文件名、文件内容。说明一下,提升工作效率的需求不一定都需要自己动手编程去实现,可以通过借助工具中已有的功能去实现。比如去掉字符串中前、中、后的空格、替换文件指定字符串等,可以借助编辑器中替换功能快速的实现。
Windows系统
1、Windows系统下替换文件内容中的字符串。比如将文件中的字符串"DTS0506"替换为"DTS0606"。
(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-96E6D:.└─DTS0506_001
DTS0506_001.txt
DTS0506_002.txt
DTS0506_003.txt
其中1个文件内容如下:
D:\dir_temp\DTS0506_001>type DTS0506_002.txt
DTS0506_001_001 DTS0506_001_001
当前需要将文文件名、目录名和文件中的内容字符串"DTS0506"替换为"DTS0606"。
脚本代码
#-*- encoding:utf-8 -*-
importsys, os, fileinput#global variable define
work_dir = u"D:\dir_temp"old_str= "DTS0506"new_str= "DTS0302"
#end
if not os.path.exists(work_dir) or notos.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 infiles:
file_path=os.path.join(root,filename)#replace old string to new string in file
repl_total =0
line_repl_count=0for 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_countprintnew_line,else:printline,#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
ifos.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 indir:
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
ifos.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 DTS0506_001.txt to DTS0606_001.txt success,, 7occurrences were replaced
Change file name DTS0506_002.txt to DTS0606_002.txt success,, 2occurrences were replaced
Change file name DTS0506_003.txt to DTS0606_003.txt success,, 7occurrences were replaced
D:\dir_temp\DTS0606_001
D:\dir_temp\DTS0506_001
Change directory name DTS0506_001 to DTS0606_001 success.请按任意键继续. . .
子目录名和文件名修改成功,结果如下:
d:\dir_temp>tree /f
卷 LENOVO 的文件夹PATH列表
卷序列号为 D892-96E6D:.└─DTS0606_001
DTS0606_001.txt
DTS0606_002.txt
DTS0606_003.txt
文件内容修改成功,如下:
d:\dir_temp\DTS0606_001>type DTS0606_002.txt
DTS0606_001_001 DTS0606_001_001
Linux系统
1、Linux替换指定目录下(包含子目录)的所有文件中的字符串
find . -name "*.txt" -print0 |xargs -n 0 sed -i 's/DTS0506/DTS0606/g'
如果仅替换当前目录下(不包含子目录)的所有文件中的字符串,可以使用如下命令
sed -i 's/DTS0506/DTS0606/g' *.txt
2、Linux中替换指定目录下的文件名。使用rename命令。下述命令表示将修改当前目录中以.txt为后缀的文件,将文件名中DTS0506字符串替换为DTS0606。
rename DTS0506 DTS0606 *.txt
说明:C语言版本的rename指令中文件名可以使用正则表达式匹配,但是新、旧字符串只能精确匹配,这就意味着如果你需要替换的字符串中有一个字符是变化的,就无法进行一次性批量修改。
如果觉得《win10修改linux文件夹名称 批量修改指定目录下的文件名和文件内容》对你有帮助,请点赞、收藏,并留下你的观点哦!