Rename files using regular expression in linux

后端 未结 7 1410
北恋
北恋 2020-12-04 14:03

I have a set of files named like:

Friends - 6x03 - Tow Ross\' Denial.srt
Friends - 6x20 - Tow Mac and C.H.E.E.S.E..s         


        
相关标签:
7条回答
  • 2020-12-04 14:58

    Edit: found a better way to list the files without using IFS and ls while still being sh compliant.

    I would do a shell script for that:

    #!/bin/sh
    for file in *.srt; do
      if [ -e "$file" ]; then
        newname=`echo "$file" | sed 's/^.*\([0-9]\+\)x\([0-9]\+\).*$/S0\1E\2.srt/'`
        mv "$file" "$newname"
      fi
    done
    

    Previous script:

    #!/bin/sh
    IFS='
    '
    for file in `ls -1 *.srt`; do
      newname=`echo "$file" | sed 's/^.*\([0-9]\+\)x\([0-9]\+\).*$/S0\1E\2.srt/'`
      mv "$file" "$newname"
    done
    
    0 讨论(0)
提交回复
热议问题