How to do a mass rename?

前端 未结 11 2020
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 06:23

I need to rename files names like this

transform.php?dappName=Test&transformer=YAML&v_id=XXXXX

to just this

XXXXX.t         


        
11条回答
  •  离开以前
    2020-11-27 06:42

    You may use whatever you want to transform the name (perl, sed, awk, etc.). I'll use a python one-liner:

    for file in 'transform.php?dappName=Test&transformer=YAML&v_id='*; do 
        mv $file `echo $file | python -c "print raw_input().split('=')[-1]"`.txt;
    done
    

    Here's the same script entirely in Python:

    import glob, os
    PATTERN="transform.php?dappName=Test&transformer=YAML&v_id=*"
    
    for filename in glob.iglob(PATTERN):
          newname = filename.split('=')[-1] + ".txt"
          print filename, '==>', newname
          os.rename(filename, newname)
    

    Side note: you would have had an easier life saving the pages with the right name while grabbing them...

提交回复
热议问题