Rename multiple files, changing prefix, extension and dropping characters

I'm currently only able to perform some very basic functions, so hope this makes sense...

I have a set of about 27 files that need to be renamed from something like this:

000012ABCDEFGHIJ.XXX.YYY.ZZZ
000078KLMNO.XXX.YYY.ZZZ
000099PQ.XXX.YYY.ZZZ

to something like this:

newa012.abc
newa078.abc
newa099.abc

The file names currently all start with '0000', then a 2-digit value, then a name that can be anywhere from 2 to 10 characthers, then the same extension. They need to be changed to a new prefix + 0 + the existing 2 digit value, dropping the next 2 to 10 characters, and changing the extension on each file to the new one. All are case sensitive as indicated.

I was able to use one-liners to get to a file name like 'newa012PQ.abc', and am having the most trouble trying to understand how to drop the 'PQ' etc.

As I said I'm not familiar with much of this and see a lot of references to sed and awk etc. but short of copying code I'm not sure the meaning of, or messing with scripting that I've never touched before I figured I'd try here first.

Any help is appreciated!
Thanks

bash

$ for file in 0000*; do new=${file#0000}; new=newa0${new:0:2}.abc; echo mv "$file" "$new"; done
mv 000012ABCDEFGHIJ.XXX.YYY.ZZZ newa012.abc
mv 000078KLMNO.XXX.YYY.ZZZ newa078.abc
mv 000099PQ.XXX.YYY.ZZZ newa099.abc

POSIX:

$ f=000012ABCDEFGHIJ.XXX.YYY.ZZZ
$ f=${f%${f#??????}}
$ echo "newa${f#???}.abc"
newa012.abc

I'm getting this error:

-bash: syntax error near unexpected token `do'

You can try this in working directory :

ls -1 | sed 's/\(^0\{1,4\}\)\([0-9][0-9]\)\(.*\)/mv & newa0\2.abc/ ' # | sh

After checkin the output pipe it to sh (remove the #) :stuck_out_tongue:

I'd suggest piping it into 'cat' instead of 'sh' first, to see if what it prints makes any sense.

@neutronscott - I'm assuming that I would use this in a script??? If this is something we'd have to run infreuqently I'm guessing that makes more sense just not sure how to do that.
Anyhoo... @Peasant - running the command in the working directory using your code works!!! I did take your advice @Corona688 - and piped to 'cat' instead of 'sh' first then via 'sh' and I see what it does, but can someone explain more what it's doing one vs the other?
Thanks to all again. This has been most helpful and very much appreciated!!!

cat shows the commands.

sh runs the commands.

I suppose that's OK if your filenames have absolutely no chance of containing whitespace or special characters. It's not good practice to parse ls or evaluate user data.

Probably the command was copied wrong. Did you try post #3?

proof of concept

$ ls -l
total 0
-rw-r--r-- 1 mute mute 0 May 17 17:34 bad file?echo fail
$ ls -1 | sed 's/\(^0\{1,4\}\)\([0-9][0-9]\)\(.*\)/mv & newa0\2.abc/ ' | sh
sh: bad: not found
fail

The filenames are the same every day but I've been reading about concerns using 'ls' so now I'm a little worried. There are other files in this directory, though they are not named anything similar in prefix or extension. When running

ls -1 | sed 's/\(^0\{1,4\}\)\([0-9][0-9]\)\(.*\)/mv & newa0\2.abc/ ' | sh

it returns

sh: line 7: ./(other file names here): Permission denied

I reran this

for file in 0000*; do new=${file#0000}; new=newa0${new:0:2}.abc; echo mv "$file" "$new"; done

[/FONT]and I was copying the $ at the beginning before but when I dropped that it allows me to see the correct results...But to use this, I'm still not understanding how I then get it to run to actually change the file names rather than just showing it?

Oh, sorry. remove the word "echo".

AWESOME. THAT'S IT!!!!

Appreciate all the help from everyone. It all helps, and will be useful as I try to piece this stuff together going forward.

Sorry for so many questions, appears I have a lot of work to do to get up to speed as a "Dummie"