How to do a mass rename?

2019-01-05 03:35发布

I need to rename files names like this

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

to just this

XXXXX.txt

How can I do it?

I understand that i need more than one mv command because they are at least 25000 files.

11条回答
孤傲高冷的网名
2楼-- · 2019-01-05 03:56

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-05 03:59

If you are using zsh you can also do this:

autoload zmv
zmv 'transform.php?dappName=Test&transformer=YAML&v_id=(*)' '$1.txt'
查看更多
等我变得足够好
4楼-- · 2019-01-05 04:02

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...

查看更多
趁早两清
5楼-- · 2019-01-05 04:02

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
查看更多
The star\"
6楼-- · 2019-01-05 04:04

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
查看更多
Fickle 薄情
7楼-- · 2019-01-05 04:10

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.

查看更多
登录 后发表回答