Shell globbing patterns are not regular expressions. They may look similar, but they work quite differently in some respects.
The regular expression [0-9a-z]{16}
matches a string that contains a run of 16 characters of the mentioned character range (the string containing the match may be longer).
The equivalent shell globbing pattern would be something like
*[0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z]*
This is because
- Globbing patterns do not support modifiers that says "repeat the previous N times".
- Globbing patterns are by default anchored to the beginning and end. This is why I inserted
*
at the start and end of the pattern. A *
matches any number of any character. Remove these if you want to match exactly 16 characters.
You may create a string of 16 [0-9a-z]
using Perl:
$ pattern="$( perl -e 'print "[0-9a-z]" x 16' )"
With bash
, you may choose to iterate over all names in the current directory, and then test whether the names match your regular expression:
for name in *; do
if [[ -f "$name" ]] && [[ "$name" =~ ^[0-9a-z]{16}$ ]]; then
echo mv "$name" "$name.txt"
fi
done
Remove the echo
once you know that it's doing the right thing.
Notice that I've anchored the pattern so that longer names won't match. I'm also making sure, with the -f
test, that the names that we get are actually those of regular files only.
for file in +([0-9a-z])
and then rename only if length of${#file}
is 16 – Sundeep Apr 09 '17 at 03:02$ echo $0
isbash
. And it's not possible to specify the number of repetitions in this case? – Hölderlin Apr 09 '17 at 03:13????????????????
in place of<regX>
, though this doesn't let you care what those sixteen characters are – Fox Apr 09 '17 at 03:31find
instead? – cylgalad Apr 09 '17 at 08:39