Linux批量替换文件名
初始化创建测试文件:# touch test_{1..10}.log# lstest_1.logtest_2.logtest_3.logtest_4.logtest_5.logtest_6.logtest_7.logtest_8.logtest_9.log test_10.log方式 一:mv使用mv进行批量替换:# ①、批量替换文件中“_”字符为"-"字符# find ./ -name "*_*
·
初始化创建测试文件:
# touch test_{1..10}.log
# ls
test_1.log test_2.log test_3.log test_4.log test_5.log test_6.log test_7.log test_8.log test_9.log test_10.log
方式 一:mv
使用mv进行批量替换:
# ①、批量替换文件中“_”字符为"-"字符
# find ./ -name "*_*" | while read f; do mv $f ${f/_/-}; done
# ②、批量替换文件中“test”字符为"TEST"字符
# ls | while read f; do mv $f ${f/test/TEST}; done
> 引申,文件中的文本替换为:
# A). 对于单个文件
# sed -i 's/string1/string2/g' example.txt
说明:将example.txt文件中的 "string1" 替换成 "string2"(如果不加-i参数,则在内存中替换; 添加-i参数,则在文件中直接替换)
# B). 对于多个文件
# $ ls|while read f; do sed -i 's/beijing/shanghai/g' $f; done
方式 二:rename
linux的rename命令有两个版本,一个是c语言版本的,一个是perl语言版本的,一般内置的都是C语言版本的
2.1 C语言版本格式:rename 原字符串 新字符串 文件名
例子:将文件名中的“test”批量替换为“TEST”
# rename test TEST test_*.log
# ls
# TEST_10.log TEST_1.log TEST_2.log TEST_3.log TEST_4.log TEST_5.log TEST_6.log TEST_7.log TEST_8.log TEST_9.log
2.2 Perl语言版本格式:rename ‘s/原字符串/新字符串/’ 文件名
perl版的rename因其支持正则表达式,使用更广泛:
例子:
字母的替换rename "s/AA/aa/" * //把文件名中的AA替换成aa
修改文件的后缀rename "s//.html//.php/" * //把.html 后缀的改成 .php后缀
批量添加文件后缀rename "s/$//.txt/" * //把所有的文件名都以txt结尾
批量删除文件名rename "s//.txt//" * //把所有以.txt结尾的文件名的.txt删掉
更多推荐
已为社区贡献16条内容
所有评论(0)