Using Perl to rename files in a directory

前端 未结 4 386
故里飘歌
故里飘歌 2021-01-12 10:56

I\'d like to take a directory and for all email (*.msg) files, remove the \'RE \' at the beginning. I have the following code but the rename fails.

opendir(D         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-12 11:03

    I don't know if the regex fits the specifig name of the files, but in one line this could be done with:

    perl -E'for (){ ($new = $_) =~ s/(^RE)(.*$)/$2/; say $_." -> ".$new}

    (say ... is nice for testing, just replace it with rename $_,$new or rename($_,$new) )

    1. <*.*> read every file in the current directory
    2. ($new = $_) =~ saves the following substitution in $new and leaves $_ as intact
    3. (^RE) save this match in $1 (optional) and just match files with "RE" at the beginning
    4. (.*$) save everything until and including the end ($) of the line -> into $2
    5. substitute the match with the string in$2

提交回复
热议问题