Search code examples
linuxbashrenamemv

Remove zeros inside file name


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.


Solution

  • 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