In the bash shell,
for file in *.TXT
do
[[ $file =~ \#([[:digit:]]+) ]] && mv -- "$file" "#$(printf '%03d' "${BASH_REMATCH[1]}") - ${file}"
done
This loops over all of the files in the current directory that end in .TXT
and compares them with bash's =~
conditional expression operator. It compares the incoming filename to the regular expression on the right. The regex looks for a hash mark (escaped, so it's not a comment), followed by some (captured in parenthesis) digits. The +
is greedy, so it will pick up as much of the number as it can (1 digit, 4 digits, etc), ending at the first non-digit (space, in your case).
Bash saves the captured number in ${BASH_REMATCH[1]}
(since it was the first set of parenthesis); we send that number to printf
in order to get it zero-padded to three digits, then append on the -
for the rest of the rename.
The rename only happens if the match was successful (via the &&
chaining).
#
-mark? – Jeff Schaller Feb 01 '19 at 18:37#
mark? – Jeff Schaller Feb 01 '19 at 18:39digit digit digit space
the digit usually are from 1 to 4
– Ancucchi Feb 01 '19 at 18:41#
will appear more than once in the filename? – jesse_b Feb 01 '19 at 18:42find
here, because they're all in various subdirectories, or are they all in the same directory? – Jeff Schaller Feb 01 '19 at 18:42