Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 将文件从.cfg更改为.txt_Linux_Bash_File - Fatal编程技术网

Linux 将文件从.cfg更改为.txt

Linux 将文件从.cfg更改为.txt,linux,bash,file,Linux,Bash,File,对于单个文件,我可以使用mv将.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'`;do

对于单个文件,我可以使用mv将.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是关于此操作(以及其他操作)的非常有用的参考: