How to rename some file of same pattern in shell s

2020-04-10 03:17发布

I want to write a code is shell scripting which will rename all the files of extension .txt in a current directory to extension .c .Suppose my current directory contains some 100 .txt file. This number is not fixed.

标签: shell
3条回答
闹够了就滚
2楼-- · 2020-04-10 03:38

awk can do this trick too:

kent$  ls *.txt|awk '{o=$0;gsub(/txt$/,"c"); print "mv "o" "$0;}'|sh
查看更多
▲ chillily
3楼-- · 2020-04-10 03:39
for f in *.txt; do echo mv "$f" "${f%.txt}.c"; done

Remove "echo" when you're satisfied it's working. See the bash manual for the meaning of "%" here.

查看更多
再贱就再见
4楼-- · 2020-04-10 03:51

See man rename. You can rename multiple files providing regexp substitution.

rename 's/\.txt$/.c/' *.txt

If you don't have rename in you system, you can use find:

find . -name '*.txt' | while read FILE; do echo mv "$FILE" "$(echo "$FILE" | sed 's/\.txt$/.c/g')"; done

Remove echo when you verify it does what you want.

查看更多
登录 后发表回答