Rename filename to another name

前端 未结 3 1328
一生所求
一生所求 2020-12-04 02:58

Really simple to explain :

How to rename

/folder1/something.test.html
/folder1/somethingElse.test.html
/folder2/againsomething.test.html
/canBeHereAl         


        
3条回答
  •  囚心锁ツ
    2020-12-04 03:39

    For simple string suffix and prefix manipulation, I suggest you familiarize yourself with the shell's standard parameter expansion features ${VAR%suffix_to_remove} and ${VAR#prefix_to_remove}. These will work on any standard sh, not just bash:

    test -> test2

    for NAME in */*.test.html; do      # NAME is, e.g., "dir/foo.test.html"
      BASENAME=${NAME%.test.html}      # BASENAME is "dir/foo"
      mv "$NAME" "$BASENAME.test2.html";
    done
    

    test2 -> test is similar:

      ...
      BASENAME=${NAME%.test2.html}      # strip off .test2.html
      ...
    

    You could also use the standard shell utilities basename and dirname to achieve something similar.

提交回复
热议问题