How to insert an underscore into a file name - UNIX

350 Views Asked by At

I'm using HP UX and I have 1000 files that are all 10 characters long and are all numbers.

I would like to put an underscore in the file name after the 4th number to make an 11 digit file name.

i.e 1234567890 to 1234_567890.

What would be the best way to do this ?

I haven't really attempted much as I don't know the best way to tackle it and have very limited unix rename knowledge.

2

There are 2 best solutions below

2
Alon Alush On

You can use the sed command

find . -type f -name "??????????" | while read f
do
    mv "$f" "$(echo $f | sed 's/^\(....\)/\1_/')"
done
0
Allan Wind On

I usually do something along these lines with bash:

find . -type f | while read f
do 
    mv "$f" "${f:0:4}_${f:4}"
done