Remove repeating pattern from beginning of file names.

I want a shell script that will traverse a file system starting at specific path.

And look at all file names for repeating sequences of [number, number, space] and remove them from the file name.

The portion of the name that gets removed has to be a repeating sequence of the same characters.

So the script would start from a specific directory, for example:

/Users/username/Music/

And traverse the file system looking at file names. Files would get renamed like this:

01 01 01 file23.txt >becomes> file23.txt
02 file717.txt >becomes> file717.txt
07 07 07 07 99 file.txt >becomes> 99 file.txt
file444.txt is unchanged

I was thinking that maybe this could be done using find piped into xargs, but I am not really sure how to do this.

Any feedback on this would be appreciated very much.

$ echo "01 01 01 file23.txt" | sed "s/\([^ ]* \)\1\{0,\}//"
file23.txt
$ echo "07 07 07 07 99 file.txt" | sed "s/\([^ ]* \)\1\{0,\}//"
99 file.txt
$  echo file444.txt | sed "s/\([^ ]* \)\1\{0,\}//"
file444.txt
$ echo "02 file717.txt" | sed "s/\([^ ]* \)\1\{0,\}//"
file717.txt

With thanks to anbu23 for the sed:

find . -type f |
while read -r f; do
    b1=${f##*/}
    b2=`echo "$b1" | sed 's/^\([0-9][0-9] \)\1\{0,\}//'`
    [ "$b1" != "$b2" ] && mv "$f" "${f%/*}/$b2"
done

If you also want to rename directories, the find invocation will need to be modified (to drop the file type check and to specify depth first traversal).

Cheers,
alister

Thank you both for the responses. The sed syntax from anbu23 is fantastic. Also, I ran the script that alister posted here on a bunch of test files and it works exactly as hoped. I will study this command structure to help me improve my scripting. Thank you!