How to do a mass rename?

前端 未结 11 1988
被撕碎了的回忆
被撕碎了的回忆 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:34

    Easiest solution is to use "mmv"

    You can write:

    mmv "long_name*.txt" "short_#1.txt"
    

    Where the "#1" is replaced by whatever is matched by the first wildcard. Similarly #2 is replaced by the second, etc.

    So you do something like

    mmv "index*_type*.txt" "t#2_i#1.txt"
    

    To rename index1_type9.txt to t9_i1.txt

    mmv is not standard in many Linux distributions but is easily found on the net.

    0 讨论(0)
  • 2020-11-27 06:36

    This should also work:

    prfx='transform.php?dappName=Test&transformer=YAML&v_id='

    ls $prfx* | sed s/$prfx// | xargs -Ipsx mv "$prfx"psx psx

    0 讨论(0)
  • 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...

    0 讨论(0)
  • 2020-11-27 06:44

    Try the rename command

    Or you could pipe the results of an ls into a perl regex.

    0 讨论(0)
  • 2020-11-27 06:45

    this renamer command would do it:

    $ renamer --regex --find 'transform.php?dappName=Test&transformer=YAML&v_id=(\w+)' --replace '$1.txt' *
    
    0 讨论(0)
  • 2020-11-27 06:47

    Ok, you need to be able to run a windows binary for this.

    But if you can run Total Commander, do this:

    1. Select all files with *, and hit ctrl-M

    2. In the Search field, paste "transform.php?dappName=Test&transformer=YAML&v_id="

      (Leave Replace empty)

    3. Press Start

    It doesn't get much simpler than that. You can also rename using regular expressions via this dialog, and you see a realtime preview of how your files are going to be renamed.

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