Linux 批量重命名-修改配置文件 shell 命令

一、批量重命名

1.1 添加前缀 / 修改后缀
[localhost@user home]$ ls
1.txt  2.txt  3.txt  4.txt  5.txt

[localhost@user home]$ for i in `ls`; do mv -f $i `echo "text_"$i`; done   # 直接修改
[localhost@user othe]$ for i in `ls`; do cp -f $i `echo "text_"$i`; done   # 复制并添加前缀
[localhost@user home]$ ls
text_1.txt  text_2.txt  text_3.txt  text_4.txt  text_5.txt


[localhost@user home]$ rename 's/\.txt/\.csv/' *
[localhost@user home]$ ls
text_1.csv  text_2.csv  text_3.csv  text_4.csv  text_5.csv
1.1 修改文件名某个字段
# 文件 hello_world.c 改为 bay_world.c
rename 's/hello/bay/' files                  #修改单个
find -name *.c | rename 's/hello/bay/' files

二、修改配置文件

Linux shell 执行修改配置文件中的内容
在开发的过程中可能Linux环境不一致需要适应本地环境的HOME目录,可以通过脚本来修改配置文件内容,写一个test.sh的脚本

示例:
如下config.txt文件内容:

[server]
listening_port=8000
proxy_url=
save_path=y
aaa_text = this line is bad
#bbb_text = this line is good

[recognize]
url=http://api.sss.com/data/invoice_query
user_code=123jaduqhejrq124wqd
auth_code=123jaduqhejrq124wqd
sed 注释与反注释

sed 's/^aaa/#&/' config.txt   # 用sed在aaa前加#注释,&的意思是匹配任意字符
sed 's/^#bbb/bbb/' config.txt

sed 内容添加与替换
sed -i -e 's|proxy_url=|proxy_url=http://10.22.123.56:5000|' config.txt
sed -i -e 's|save_path=y|save_path=n|' config.txt
sed -i -e 's|user_code=123jaduqhejrq124wqd|user_code=123jaduqhejrq124qqq|' config.txt
sed -i -e 's|auth_code=123jaduqhejrq124wqd|auth_code=123jaduqhejrq124qqq|' config.txt
在脚本里写入以下命令:
sed -i "s#ftfts_com_serverpa_path=.*#ftfts_com_serverpa_path= $HOME#g" test.properties

该命令的基本语法如下:
sed -i "s/要替换的内容/替换后的内容/g" 文件名

使用`#`代替/能够适应替换内容中含有/的内容,不需要转译.不然还要使用//转译!.*是匹配所有内容!

三、常用命令

当前路径,输出日期格式,拼接文件名,正则获取最新的文件夹/文件

current="/home/xxx/"
echo "当前路径: $current"
cd $current

date_name=`date +%Y_%m_%d_%H_%m_%S`

dir_name="filenme_${date_name}"

laster_dir=`ls -lt|grep "filename_2020_"|awk '{print $9}'|awk 'NR==1'`
echo "刚才创建的文件夹: $laster_dir"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongpingWang

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值