Bash command to remove leading zeros from all file names

前端 未结 8 1831
一整个雨季
一整个雨季 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:13

    I dont know sed at all but you can get a listing by using find:

    find -type f -name *.jpg

    so with the other answer it might look like

    find . -type f -name *.jpg | sed -e 's:^0*::'

    but i dont know if that sed command holds up or not.

    0 讨论(0)
  • 2021-02-09 00:17

    Try using sed, e.g.:

    sed -e 's:^0*::'
    

    Complete loop:

    for f in `ls`; do
       mv $f $(echo $f | sed -e 's:^0*::')
    done
    
    0 讨论(0)
  • 2021-02-09 00:21
    for FILE in `ls`; do mv $FILE `echo $FILE | sed -e 's:^0*::'`; done
    
    0 讨论(0)
  • 2021-02-09 00:23

    In Bash, which is likely to be your default login shell, no external commands are necessary.

    shopt -s extglob
    for i in 0*[^0]; do mv "$i" "${i##*(0)}"; done
    
    0 讨论(0)
  • 2021-02-09 00:31

    Maybe not the most elegant but it will work.

    for i in 0*
    do
    mv "${i}" "`expr "${i}" : '0*\(.*\)'`"
    done
    
    0 讨论(0)
  • 2021-02-09 00:34

    sed by itself is the wrong tool for this: you need to use some shell scripting as well.

    Check Rename multiple files with Linux page for some ideas. One of the ideas suggested is to use the rename perl script:

    rename 's/^0*//' *.jpg
    
    0 讨论(0)
提交回复
热议问题