Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a mass rename?

Tags:

shell

unix

rename

People also ask

How do I batch edit file names?

To batch rename files, just select all the files you want to rename, press F2 (alternatively, right-click and select rename), then enter the name you want on the first file. Press Enter to change the names for all other selected files.

How do I bulk rename photos?

Mass rename images on Windows You can batch rename images in Windows by selecting (Shift+click or Ctrl+click to select several files; Ctrl+A to select all) and pressing right-click > "Rename".

How do I bulk rename a PDF in Excel?

Open the export list and copy your naming in some Excel sheet into the column of "NewName"; Select "File->Import List" to import the edited list with new naming; Click "Rename" to batch change multile PDF files at the same time.


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.


If you are using zsh you can also do this:

autoload zmv
zmv 'transform.php?dappName=Test&transformer=YAML&v_id=(*)' '$1.txt'

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

Try the rename command

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


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


find -name '*v_id=*' | perl -lne'rename($_, qq($1.txt)) if /v_id=(\S+)/'