Linux rename files to uppercase

The bash shell has a syntax for translating a variable name to all-caps.

for file in * ; do      # or *.jpg, or x*.jpg, or whatever
    mv "$file" "${file^^}"
done

This feature was introduced in bash version 4.0, so first verify that your version of bash implements it. To avoid mistakes, try it once replacing mv by echo mv, just to make sure it's going to do what you want.

The documentation for this feature is here, or type info bash and search for "upper".

You should probably decide what to do if the target file already exists (say, if both x00000.jpg and X00000.JPG already exists), unless you're certain it's not an issue. To detect such name collisions, you can try:

ls *.txt | tr '[a-z]' '[A-Z]' | sort | uniq -c | sort -n

and look for any lines not starting with 1.


rename

Probably the easiest way for renaming multiple files is using Perl's rename. To translate lowercase names to upper, you'd:

rename 'y/a-z/A-Z/' *

If the files are also in subdirs you can use globstar or find:

find . -maxdepth 1 -type f -iname "*.jpg" -execdir rename "y/a-z/A-Z/" {} +

References

  • Howto: Linux Rename Multiple Files At a Shell Prompt – nixCraft
  • More info about y/, translate instead of substitute.
  • DistroTube - Tools For Renaming Files In Linux

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.

Tags:

Linux

Bash

Ubuntu