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
"${f^^}"
would have been enough.
Commented
Nov 27, 2013 at 21:58
touch l u; tr [:lower:] [:upper:] <<< "hello world"
Commented
Nov 27, 2013 at 22:21
x00000.jpg
to X00000.JPG
though OP seems to be wanting X00000.jpg
-s
option to tr
looks like a bug to me that can destroy data.
Commented
Nov 28, 2013 at 1:55
tr [:lower:] [:upper:]
does not (yet) handle multi-byte characters. The same applies to the proposed ${f^^}
in bash (fixed in bash 4.3, which is not released yet). sed
works, at least on my machine.
Commented
Nov 30, 2013 at 23:41
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.
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
.
4.1.5(1)-release (x86_64-pc-linux-gnu)
of bash...
${file^^}
treats $file
as a single string. The extension is just part of the file name. But ask yourself why you want to do this. Linux supports upper, lower, and mixed case, but lower case is conventional.
Commented
Jan 15, 2018 at 1:00
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/" {} +
y/
, translate instead of substitute.Combining previous answers could yield:
for file in * ; do # or *.jpg, or x*.jpg, or whatever
basename=$(tr '[:lower:]' '[:upper:]' <<< "${file%.*}")
newname="$basename.${file#*.}"
mv "$file" "$newname"
done
Using tr:
f="x00000.jpg"
n="${f%.*}"
n=$(tr '[:lower:]' '[:upper:]' <<< "$n")
f="$n.${f#*.}"
echo "$f"
OUTPUT:
X00000.jpg
[a-z]
are locale-dependent and using them might create hidden bugs. Also, your solution does not handle multi-byte characters.
Commented
Nov 30, 2013 at 23:47
multi-byte
requirement in OP's question but I edited.
tr '[:lower:]' ...
does not handle multi-byte characters as well. This is really bad. :( (Oh, and you better quote [:lower:]
here, because otherwise the shell might expand it to a filename. :)
Commented
Dec 1, 2013 at 14:02
#!/bin/bash
SOURCE_DIRS=('./src' './Public' './Private')
FILE_EXTENSION="*.php"
for dir in $SOURCE_DIRS; do
files="$(find "$dir" -name "${FILE_EXTENSION[@]}";)"
for file in $files; do
base_name="$(basename $file)";
new_name=$(sed "s/$base_name/${base_name^}/g" <<< $file)
git mv $file ${new_name} # or
# mv $file ${new_name}
done
done
x00000.jpg
to replace the originalX00000.jpg
?