win10修改linux文件夹名称,批量修改指定目录下的文件名和文件内容

需求

批量修改替换Linux/Windows系统中指定目录下的目录名、文件名、文件内容。说明一下,提升工作效率的需求不一定都需要自己动手编程去实现,可以通过借助工具中已有的功能去实现。比如去掉字符串中前、中、后的空格、替换文件指定字符串等,可以借助编辑器中替换功能快速的实现。

Windows系统

1、Windows系统下替换文件内容中的字符串。比如将文件中的字符串"DTS20180506"替换为"DTS20190606"。

(1)使用Notepad++的文件内容替换功能。操作步骤:打开Notepad++ --> 搜索 --> 在文件中查找(也可以使用快捷键CTRL+f),界面和使用示例如下:

9fb36914e6c4ce244724a6cb7c37d608.png

同时,在这里再介绍一下Windows下操作文本的几个主要场景:去掉行首空格、行尾空格、行首行尾全部空格,在行首添加字符串,在行尾添加字符串。通过使用正则表达式+文本处理器(Notepad++、UE等)完成

去掉行首空格、行尾空格、行首行尾全部空格。下图表示替换行首空格。如果是替换行尾空格,查找目标输入为([ \t]$)。注意勾选查找模式中的正则表达式。

2fcd984146c6ce8c8e01abb5c28165ef.png

如果是Notepad++编辑器,也可以使用菜单中的功能实现。路径如下:Notepad++主界面 --> 编辑 --> 空白字符操作。

13c984dd19f15955e4c1c8a2da36e34e.png

在行首添加字符串。在查找目标中输入^,替换目标中输入字符串。如需要在每行记录前面添加字符串root,示例如下

6678341f3c05fadcd0c6769169dc1720.png

在行尾添加字符串。在查找目标中输入$,替换目标中输入字符串。如需要在每行行尾添加字符串end。示例如下

8fb87b22605e3f0b40912ffe7bd0af54.png

2、Windows下修改指定目录下的文件名和目录名。假设指定目录为D:\dir_temp,其子目录结构如下:

D:\dir_temp>tree /f

卷 LENOVO 的文件夹PATH列表

卷序列号为 D892-96E6D:.└─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 -*-

importsys, 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 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 DTS20180506_001.txt to DTS20190606_001.txt success,, 7occurrences were replaced

Change file name DTS20180506_002.txt to DTS20190606_002.txt success,, 2occurrences were replaced

Change file name DTS20180506_003.txt to DTS20190606_003.txt success,, 7occurrences 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-96E6D:.└─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指令中文件名可以使用正则表达式匹配,但是新、旧字符串只能精确匹配,这就意味着如果你需要替换的字符串中有一个字符是变化的,就无法进行一次性批量修改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值