Remove zeros inside file name

1.2k views Asked by At

I have this file name

1006_12_000123123_000023126.data

and I want this file name. I have arround 300000 files.

1006_12_123123_23126.png

I tried som of these solution, but they are for filename like 00002323.jpg

Bash command to remove leading zeros from all file names

I can use mv to rename.

2

There are 2 answers

2
ron rothman On BEST ANSWER
for original_name in *.data; do
    # determine new file name from original:
    # remove zeroes and change extension.
    new_name=$(echo "$original_name" | sed -e 's/_0*/_/g' -e s'/.data$/.png/')
    mv "$original_name" "$new_name"
done
3
Eduardo Escobar On

Use this

ls * | sed -e 'p;s/_0*/_/g' | xargs -n2 mv