Renaming large number of files.
Hello ladies and gents,
I have a very large number (~500) of files I need to rename. I suppose I could write a perl script to do it... but I'm looking for something quick and dirty. I need to rename files that look like this: EM76540.pdf to this: 76540EM.pdf Can I use bash with mv or am I doing to have to write some perl script? I wish you could just type mv EM*.pdf *EM.pdf That would be awesome. |
There is a rename command that can do bulk renames.
I will use variable expansion instead. Code:
for file in EM*.pdf; do Code:
for file in EM*pdf; do |
I usually refer to this article for such tasks and can usually figure out how to come up with the right command through trial and error on some test subjects relatively quickly.
|
Thank you both!
jschiwal that simple script did exactly what I needed. I can see why you're a guru. I really appreciate it. For me to do that with a perl script would have taken me hours I'm sure. I haven't had a chance to visit that site you mentioned Junior Hacker - it stated it was too busy. But it looks like something I will definitely look at later. Man... I need to learn more scripting. That's a handy tool. |
Renaming in bulk, more complicated example
Can anyone provide some advice on a more complicated example to rename files...
I have files in the example format: Babypicture_playing_with_spoon_date=20070602 Where the front of the file name is always a constant number of characters (here its 11 'Babypicture'), the middle is a descriptor, and the end is a constant number of characters for the date (14 characters, _date=20070602), with no end descriptor (.jpg is not there) How can I rename these, using perl, to Babypicture_date=20070602.jpg Where I am cutting out the middle descriptor and adding the .jpg at the end? Thanks |
I don't know using Perl, but in Bash you can do something like
Code:
file="Babypicture_playing_with_spoon_date=20070602" |
|
Thanks Colucix.
Will try it, but hopefully someone can guide me on some perl code for this too. Seem like bash fails for me when the list of files is to long - I get the "argument list to long" error |
Perhaps a little more guidance...
I ran the commands on the command line and also in a file #/bin/sh file="Baby*" newfile=${file:0:11}${file:$((${#file}-14))}.jpg No file names are changed and there is no output of errors. What am I doing wrong? Thanks |
Quote:
Code:
file="Baby*" |
yes, I actually had done:
mv file="Baby*" newfile=${file:0:11}${file:$((${#file}-14))}.jpg but was returned an error: mv: cannot stat `file=Baby*': No such file or directory Recommendation on the placement of mv? Any usage with other file notation such as ()/\{} or others? Thanks |
Of course. Mine was only a little guidance on how to build the new file name in BASH, based on your requirements. You have to embed it in a little script which retrives all the file names and rename them one at a time.
Note: at this point, since it looks like you are not experienced in shell programming, you have to do a backup of your pictures/files before trying to rename them! A simple script could be Code:
#!/bin/bash This code works only 1) if the files are all in the working directory and 2) if file names don't contain blank spaces, otherwise you have to slightly modify the code to let it work properly. |
Quote:
|
Occasionally, I do attempt living on the edge :D. But in this case, I copied some sample files to a test directory in order to perfect the renaming command.
|
He he! :) Very good!
|
All times are GMT -5. The time now is 12:39 PM. |