Linux shell script to add leading zeros to file names

后端 未结 10 1630
梦如初夏
梦如初夏 2020-12-04 06:43

I have a folder with about 1,700 files. They are all named like 1.txt or 1497.txt, etc. I would like to rename all the files so that all the filena

10条回答
  •  感动是毒
    2020-12-04 07:18

    One-liner:

    ls | awk '/^([0-9]+)\.txt$/ { printf("%s %04d.txt\n", $0, $1) }' | xargs -n2 mv
    

    How do I use grep to only match lines that contain \d.txt (IE 1 digit, then a period, then the letters txt)?

    grep -E '^[0-9]\.txt$'
    

提交回复
热议问题