Rename filename to another name

前端 未结 3 1306
一生所求
一生所求 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.

    0 讨论(0)
  • 2020-12-04 03:40

    You may use rename command.

    rename 's/(.*)\./$12./' *.html
    

    or

    rename 's/.*\K\./2./' *.html
    
    0 讨论(0)
  • 2020-12-04 03:48

    You can do this:

    name1="/folder1/something.test.html"
    name2="${name1/%.html/2.html}"
    echo "$name2"
    #mv "$name1" "$name2"
    

    Output:

    /folder1/something.test2.html
    

    .html is being replaced with 2.html at the end of the string.

    To go back i.e test2 -> test:

    name2="/folder1/something.test2.html"
    name1="${name2/%2.html/.html}"
    

    See the Bash Manual for more info on Parameter Expansion.

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