How to do a mass rename?

前端 未结 11 1990
被撕碎了的回忆
被撕碎了的回忆 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条回答
  • If you are using zsh you can also do this:

    autoload zmv
    zmv 'transform.php?dappName=Test&transformer=YAML&v_id=(*)' '$1.txt'
    
    0 讨论(0)
  • 2020-11-27 06:51
    find -name '*v_id=*' | perl -lne'rename($_, qq($1.txt)) if /v_id=(\S+)/'
    
    0 讨论(0)
  • 2020-11-27 06:51

    vimv lets you rename multiple files using Vim's text editing capabilities.

    Entering vimv opens a Vim window which lists down all files and you can do pattern matching, visual select, etc to edit the names. After you exit Vim, the files will be renamed.

    [Disclaimer: I'm the author of the tool]

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

    You write a fairly simple shell script in which the trickiest part is munging the name.

    The outline of the script is easy (bash syntax here):

    for i in 'transform.php?dappName=Test&transformer=YAML&v_id='*
    do
        mv $i <modified name>
    done
    

    Modifying the name has many options. I think the easiest is probably an awk one-liner like

    `echo $i  |  awk -F'=' '{print $4}'`
    

    so...

    for i in 'transform.php?dappName=Test&transformer=YAML&v_id='*
    do
        mv $i `echo $i |  awk -F'=' '{print $4}'`.txt 
    done
    

    update

    Okay, as pointed out below, this won't necessarily work for a large enough list of files; the * will overrun the command line length limit. So, then you use:

    $ find . -name 'transform.php?dappName=Test&transformer=YAML&v_id=*' -prune -print |
    while read
    do
        mv $reply `echo $reply |  awk -F'=' '{print $4}'`.txt 
    done
    
    0 讨论(0)
  • 2020-11-27 06:59

    I'd use ren-regexp, which is a Perl script that lets you mass-rename files very easily.

    21:25:11 $ ls
    transform.php?dappName=Test&transformer=YAML&v_id=12345
    
    21:25:12 $ ren-regexp 's/transform.php.*v_id=(\d+)/$1.txt/' transform.php*
    
      transform.php?dappName=Test&transformer=YAML&v_id=12345
    1 12345.txt
    
    
    21:26:33 $ ls
    12345.txt
    
    0 讨论(0)
提交回复
热议问题