【发布时间】:2021-05-05 23:46:40
【问题描述】:
我可以将单个文件的 .cfg 更改为 .txt >
[root@cal]# mv abc.cfg abc.txt
[root@cal]# ls | grep abc.txt
abc.txt
【问题讨论】:
我可以将单个文件的 .cfg 更改为 .txt >
[root@cal]# mv abc.cfg abc.txt
[root@cal]# ls | grep abc.txt
abc.txt
【问题讨论】:
这应该适用于大多数 shell。
如果您想在不调用 sed 的情况下进行操作,那么 bash 中有模式替换,但是......那么我将不得不在谷歌上搜索该语法。
for x in *.cfg;do mv $x `echo $x | sed -e 's/cfg/txt/g'`;done
【讨论】:
您可以使用 for 循环 对匹配模式的文件列表进行操作:
for file in *.cfg
do
mv $file "${file%cfg}txt"
done
或者,在一行中:
for file in *.cfg; do mv $file "${file%cfg}txt"; done
百分号运算符在用作 shell 变量的一部分时,会从字符串末尾删除 cfg。 bash howto 是关于此(和其他操作)的非常有用的参考:https://tldp.org/LDP/abs/html/string-manipulation.html
【讨论】: