How to rename some file of same pattern in shell scripting

前端 未结 3 1614
醉话见心
醉话见心 2021-01-21 06:50

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 10

相关标签:
3条回答
  • 2021-01-21 07:13

    awk can do this trick too:

    kent$  ls *.txt|awk '{o=$0;gsub(/txt$/,"c"); print "mv "o" "$0;}'|sh
    
    0 讨论(0)
  • 2021-01-21 07:26

    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.

    0 讨论(0)
  • 2021-01-21 07:28
    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.

    0 讨论(0)
提交回复
热议问题