Bash command to remove leading zeros from all file names

前端 未结 8 1832
一整个雨季
一整个雨季 2021-02-08 23:35

I have a directory with a bunch of files with names like:

001234.jpg
001235.jpg
004729342.jpg

I want to remove the leading zeros from all file

相关标签:
8条回答
  • 2021-02-09 00:37

    Here's one that doesn't require sed:

    for x in *.jpg ; do let num="10#${x%%.jpg}"; mv $x ${num}.jpg ;  done
    

    Note that this ONLY works when the filenames are all numbers. You could also remove the leading zeros using the shell:

    for a in *.jpg ; do dest=${a/*(0)/} ; mv $a $dest ; done
    
    0 讨论(0)
  • 2021-02-09 00:37

    In Bash shell you can do:

    shopt -s nullglob
    for file in 0*.jpg
    do
       echo mv "$file" "${file##*0}"
    done
    
    0 讨论(0)
提交回复
热议问题