rename multiple filename.45267.txt to >> filename.txt

i have several thousand files and in subdirs that are named

file.46634.txt
budget.75346.pdf

etc

i want to remove the number but retain the extension.

it is always a 5 digit.

thanks.

Okay but will you have collisions - budget.12345.pdf and budget.34567.pdf will both end up as budget.pdf?

I recommend taking this problem in 3 steps.

Create list.
Create mv commands.
Run mv commands.

If you try to do everything at once, you can encounter many
unexpected results.

So, step1: create a list of the files you want to rename:

find $PWD -type f -print |
grep '[0-9][0-9][0-9][0-9][0-0]' > file_list

Next, inspect the file_list file and make sure that you only grabbed
what you expect.

Then run this script:

awk '{ print $1, $1 }' file_list |
while read a b; do

echo $b | sed -e 's/[0-9][0-9][0-9][0-9][0-9]\.\(...\)$/\1/' | read c
echo /bin/mv $a $c
done |
tee step2

What this does is create a list of mv commands and stores then in step2.
After inspecting step2, to make sure it looks correct...
Run it:

ksh step2

there you go.

hey thanks for that. there is a slight proble however. spaces in filenames are not considered. how to fix that?

thanks again.

Aurgh. My solution was kind of stupid.... Try this:

cat file_list |
while read a; do

c=$( echo "$a" | sed -e 's/[0-9][0-9][0-9][0-9][0-9]\.\(...\)$/\1/' )
echo /bin/mv \"$a\" \"$c\"
done |
tee step2

Then, inspect step2 and run it:

/bin/ksh step2

Actually, this might work even better.
echo's tend to swallow up consecutive spaces.

cat file_list |
while read a; do

#-------------------------------------------------------
### some ksh variable editting that "greedy" removes from
### beginning to last /
#-------------------------------------------------------
 
c=${a##*/}
echo /bin/mv \"$a\" \"$c\"
done |
tee step2

thank you!

for you