Rename all files in directory from $filename_h to $filename_half?

前端 未结 11 889
执念已碎
执念已碎 2020-12-02 03:45

Dead simple.

How do I rename

05_h.png
06_h.png

to

05_half.png
06_half.png

At least, I think it\

相关标签:
11条回答
  • 2020-12-02 04:23

    Are you looking for a pure bash solution? There are many approaches, but here's one.

    for file in *_h.png ; do mv "$file" "${file%%_h.png}_half.png" ; done
    

    This presumes that the only files in the current directory that end in _h.png are the ones you want to rename.

    Much more specifically

    for file in 0{5..6}_h.png ; do mv "$file" "${file/_h./_half.}" ; done
    

    Presuming those two examples are your only. files.

    For the general case, file renaming in has been covered before.

    0 讨论(0)
  • 2020-12-02 04:28
    for f in *.png; do
      fnew=`echo $f | sed 's/_h.png/_half.png/'`
      mv $f $fnew
    done
    
    0 讨论(0)
  • 2020-12-02 04:30

    Use the rename utility:

    rc@bvm3:/tmp/foo $ touch 05_h.png 06_h.png
    rc@bvm3:/tmp/foo $ rename 's/_h/_half/' * 
    rc@bvm3:/tmp/foo $ ls -l
    total 0
    -rw-r--r-- 1 rc rc 0 2011-09-17 00:15 05_half.png
    -rw-r--r-- 1 rc rc 0 2011-09-17 00:15 06_half.png
    
    0 讨论(0)
  • 2020-12-02 04:31

    Try rename command:

    rename 's/_h.png/_half.png/' *.png
    

    Update:

    example usage:

    create some content

    $ mkdir /tmp/foo
    $ cd /tmp/foo
    $ touch one_h.png two_h.png three_h.png
    $ ls 
    one_h.png  three_h.png  two_h.png
    

    test solution:

    $ rename 's/_h.png/_half.png/' *.png
    $ ls
    one_half.png  three_half.png  two_half.png
    
    0 讨论(0)
  • 2020-12-02 04:33

    Use the rename utility written in perl. Might be that it is not available by default though...

    $ touch 0{5..6}_h.png
    
    $ ls
    05_h.png  06_h.png
    
    $ rename 's/h/half/' *.png
    
    $ ls
    05_half.png  06_half.png
    
    0 讨论(0)
  • 2020-12-02 04:34
    for i in *_h.png ; do
      mv $i `echo "$i"|awk -F'.' '{print $1"alf."$2}'`
    done
    
    0 讨论(0)
提交回复
热议问题