0

I can change .cfg to .txt with mv for a single file, but is there a quicker way to do it for multiple files at once.

[root@cal]# mv abc.cfg abc.txt

[root@cal]# ls | grep abc.txt

abc.txt
moo
  • 1,597
  • 1
  • 14
  • 29

2 Answers2

1

You can use a for loop to operate on a list of files matching a pattern:

for file in *.cfg
do
   mv $file "${file%cfg}txt"
done

Or, in one line:

for file in *.cfg; do mv $file "${file%cfg}txt"; done

the percent operator, when used as part of a shell variable, removes cfg from the end of the string. The bash howto is a very useful reference on this (and other operations): https://tldp.org/LDP/abs/html/string-manipulation.html

moo
  • 1,597
  • 1
  • 14
  • 29
0

This should work in most shells.

There is pattern replacement in bash if you want to make do without a call to sed, but... then I would have to google that syntax.

for x in *.cfg;do mv $x `echo $x | sed -e 's/cfg/txt/g'`;done
Nicholas Rees
  • 723
  • 2
  • 9