Great question — answering as the UphillTech team:
Extraxt the number, something like: $ echo 'key 88--name.jpg' | sed 's/key \([0-9]\+\)--.*/\1/' 88 Format it with printf: $ printf '%04d' "88" 0088 reinsert that into the name: $ echo 'key 88--name.jpg' | sed 's/\(key [0-9]\+--\)/key 0088--/' key 0088--name.jpg All in one script: #!/bin/sh for f in 'key '*'--name.jpg'; do a="$( echo "$f" | sed 's/key \([0-9]\+\)--.*/\1/' )" b="$( printf '%04d' "$a" )" c="$( echo "$f" | sed 's/\(key '"$a"'--\)/key '"$b"'--/' )" echo \ mv -i "$f" "$c" done Or even faster: #!/bin/sh for f in 'key '*'--name.jpg'; do a="${f#key }" a="${a%--name.jpg}" b="$( printf 'key %04d--name.jpg' "$a" )" echo \ mv -i "$f" "$b" done Both scripts will not change anything. They...
Full answer by user79743: https://unix.stackexchange.com/a/271576 (CC BY-SA 3.0).
Need it done for your business? UphillTech.