5

I have thousands of file with file extensions like this

3_bedroom_villas_in_chennai.html__201308050010_
3_bedroom_villas_in_chennai.html__201308080012_
3_bedroom_villas_in_chennai.html__201308100012_
3_bedroom_villas_in_chennai.html__201308110034_ and so on.....

inside a directory. I wanna change all these into the following

3_bedroom_villas_in_chennai__201308050010_.html
3_bedroom_villas_in_chennai__201308080012_.html
3_bedroom_villas_in_chennai__201308100012_.html
3_bedroom_villas_in_chennai__201308110034_.html

When I tried doing it in windows using the following command

ren *.* *.html

I got the following

A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found. and so on...

Because I know that it will try to change everything into a single file name like

3_bedroom_villas_in_chennai.html and so on... 

any ways to do this either on windows or linux ??

Venkateshwaran Selvaraj
  • 1,745
  • 8
  • 30
  • 60

2 Answers2

8

In Linux:

ls | xargs -I % mv % %.html

ls command output is piped to xargs, and xargs replaces all % (after mv) with the input from ls

Also, if you want recursively go through all sub-directories, you might want to use:

find . -type f | xargs -I % mv % %.html

And in Windows:

for /r %x in (*) do ren "%x" *.txt
Davi Lima
  • 800
  • 1
  • 6
  • 20
julumme
  • 2,326
  • 2
  • 25
  • 38
  • It worked .. thanks:) May I know how to do the same in windows? – Venkateshwaran Selvaraj Sep 19 '13 at 06:41
  • 1
    @Venky I added a windows solution. (Tested in Windows 7) – julumme Sep 19 '13 at 06:49
  • @julumme can you please explain what each part of `ls | xargs -I % mv % %.html` does? I mean why is `-I` used? – Ranveer Feb 12 '14 at 09:27
  • Also, what if I want to rename a specific extension? – Ranveer Feb 12 '14 at 09:57
  • @Ranveer, the `-I` defines the string to be replaced. In this example I used `%`-sign. It does not have to be a single character, but usually it's easier to understand what is going on. So it means that xargs will replace all further occurrences of `%` signs for the input from `ls` (or `find`) command. So for example `echo 1 | xargs -I % echo %%%` will print `111` to the console – julumme Feb 13 '14 at 07:43
1

using renamer:

$ renamer --regex --find '(.*).html(.*)' --replace '$1$2.html' *

Works on Windows, Mac and Linux.

Lloyd
  • 8,204
  • 2
  • 38
  • 53