tags:

views:

1202

answers:

8

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.

Thanks!

+4  A: 

Try the rename command

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

brien
And how exactly do you do that with the `rename` command along?
J.F. Sebastian
8jean
@8jean: As I understand the question the result should be: s/.*?v_id=(.*)/$1.txt/. I don't see that in your comment.
J.F. Sebastian
Ah, I missed the '.txt' extension. Seems like there's no easy way to add that using "rename".
8jean
+9  A: 

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
Charlie Martin
+1, this is the general approach.
Joshua
there is a command for this called 'rename' in most linux distros. See briens answer.
8jean
Maybe so, but not all UNIX systems (not on my Mac for example) *and* this is a general pattern that can be applied to other issues.
Charlie Martin
+4  A: 

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

Federico Ramponi
Hah hah, my awk program is shorter. ;-)
Charlie Martin
Right :) awk is perfect for this kinds of jobs. Unfortunately, neither your sh script nor mine will work for 100000 files (there's a limit to a command line length, and the * may break such limit). If this is the case, rename or a dedicate script are better solutions.
Federico Ramponi
@Federico: I'd add that the Python script (`glob.iglob()`) has a better chance to work for 100000 files.
J.F. Sebastian
+7  A: 

If you are using zsh you can also do this:

autoload zmv
zmv 'transform.php?dappName=Test&transformer=YAML&v_id=(*)' '$1.txt'
d0k
He uses bash, he said so...
bortzmeyer
He did? Do you mean the tag? It was not added by him
d0k
Even if he's using bash he can always just run zsh and then use this answer.
PEZ
+4  A: 

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.

nimrodm
There is a mmv package for Debian, for Ubuntu, for Gentoo...
bortzmeyer
The mmv command is missing the second wild card. It should bemmv "index*type*.txt" "t#2_i#1.txt"
W_Whalley
A: 

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.

Wouter van Nifterick
He tagged his question "unix"...
bortzmeyer
I'm accessing unix systems from my windows desktop all the time... Just suggesting how I would've solved this.
Wouter van Nifterick
+3  A: 
find -name '*v_id=*' | perl -lne'rename($_, qq($1.txt)) if /v_id=(\S+)/'
J.F. Sebastian
We could practically make a web page devoted to various ways of doing this.
Charlie Martin
A: 

Thank you very much guys!

Edit the question, do not post an answer just to thanks.
bortzmeyer