I have large number of files in the format x00000.jpg
, X00000.jpg
and xx00000.jpg
.
How can I rename these files so they are all uppercase, ignoring the numeric part of the name?
for f in * ; do mv -- "$f" "$(tr [:lower:] [:upper:] <<< "$f")" ; done
You can't rename files from Bash only, because Bash doesn't have any built-in command for renaming files. You have to use at least one external command for that.
If Perl is allowed:
perl -e 'for(@ARGV){rename$_,uc}' *.jpg
If Python is allowed:
python -c 'import os, sys; [os.rename(a, a.upper()) for a in sys.argv[1:]]' *.jpg
If you have thousands or more files, the solutions above are fast, and the solutions below are noticably slower.
If AWK, ls
and mv
are allowed:
# Insecure if the filenames contain an apostrophe or newline!
eval "$(ls -- *.jpg | awk '{print"mv -- \x27"$0"\x27 \x27"toupper($0)"\x27"}')"
If you have a lots of file, the solutions above don't work, because *.jpg
expands to a too long argument list (error: Argument list too long).
If tr
and mv
are allowed, then see damienfrancois' answer.
If mv
is allowed:
for file in *; do mv -- "$file" "${file^^}"; done
Please note that these rename .jpg
to .JPG
at the end, but you can modify them to avoid that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With