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?
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?
The
bash
shell has a syntax for translating a variable name to all-caps.I think this may be a fairly new feature, so first verify that your version of
bash
implements it. To avoid mistakes, try it once replacingmv
byecho 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
andX00000.JPG
already exists), unless you're certain it's not an issue. To detect such name collisions, you can try:and look for any lines not starting with
1
.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:
If Python is allowed:
If you have thousands or more files, the solutions above are fast, and the solutions below are noticably slower.
If AWK,
ls
andmv
are allowed: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
andmv
are allowed, then see damienfrancois' answer.If
mv
is allowed:Please note that these rename
.jpg
to.JPG
at the end, but you can modify them to avoid that.rename
Probably the easiest way for renaming multiple files is using
rename
. To translate lowercase names to upper, you'd:If the files are also in subdirs you can use globstar or
find
:References
y/
.Using tr:
OUTPUT:
If only renaming files/dirs is all you want, then you can use rnm :
Explanation:
-rs
: replace string././\C/g
replaces all match of.
(regex) to it's uppercase.-fo
: file only mode-dp
: depth of directory (-1 means unlimited).More examples can be found here.
Combining previous answers could yield: