Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux rename files to uppercase

Tags:

linux

bash

ubuntu

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?

like image 301
Jay Avatar asked Sep 07 '25 13:09

Jay


2 Answers

for f in * ; do mv -- "$f" "$(tr [:lower:] [:upper:] <<< "$f")" ; done
like image 106
damienfrancois Avatar answered Sep 09 '25 09:09

damienfrancois


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.

like image 37
pts Avatar answered Sep 09 '25 10:09

pts