27

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?

3
  • Do you want the renamed x00000.jpg to replace the original X00000.jpg?
    – chepner
    Commented Nov 27, 2013 at 21:48
  • Linux tends to like lowercase file names. Why do you want everything in uppercase? Commented Nov 27, 2013 at 21:52
  • honestly, it was either upper or lower and I picked upper at random. I'm using the images as part of a url, which I'm linking to from an ID provided in a data feed. the ID matches the image name. Unfortunatly, the feed data and image file data don't match up case-wise.
    – Jay
    Commented Nov 27, 2013 at 21:55

9 Answers 9

31
for f in * ; do mv -- "$f" "$(tr [:lower:] [:upper:] <<< "$f")" ; done
7
  • 2
    But why a subshell, a fork and a here-string? "${f^^}" would have been enough. Commented Nov 27, 2013 at 21:58
  • 2
    The arguments to tr should be quoted too. Consider touch l u; tr [:lower:] [:upper:] <<< "hello world" Commented Nov 27, 2013 at 22:21
  • 1
    Will that not rename x00000.jpg to X00000.JPG though OP seems to be wanting X00000.jpg
    – anubhava
    Commented Nov 27, 2013 at 22:26
  • Passing the -s option to tr looks like a bug to me that can destroy data. Commented Nov 28, 2013 at 1:55
  • It should also be noted that 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
20

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.

2
  • Word of caution, the Perl command will also make the extension upper case so pdf/zip files will become PDF/ZIP Commented Jun 17, 2015 at 23:32
  • @ParikTiwari: This is true for all commands in this answer, and also for most commands in other answers as well.
    – pts
    Commented Jun 19, 2015 at 7:51
16

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.

2
  • This works for me, but I wanted the file extension to also change to all upper case characters. FYI, I'm still (shamefully) rocking version 4.1.5(1)-release (x86_64-pc-linux-gnu) of bash...
    – Digger
    Commented Jan 14, 2018 at 18:34
  • 1
    @Digger: That's exactly what it does. ${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
15

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

8

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
1
  • 2
    this works and doesn't mess up the file extension case, i.e. it stays .jpg not .JPG
    – ski_squaw
    Commented Oct 2, 2015 at 16:33
3

Using tr:

f="x00000.jpg"
n="${f%.*}"
n=$(tr '[:lower:]' '[:upper:]' <<< "$n")
f="$n.${f#*.}"
echo "$f"

OUTPUT:

X00000.jpg
3
  • 1
    Character classes like [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
  • Yes that's a fair point though I didn't see multi-byte requirement in OP's question but I edited.
    – anubhava
    Commented Dec 1, 2013 at 4:30
  • 1
    Multi-byte filenames weren't mentioned by the OP, but most filenames are created by humans, so this should be handled by a solution. That said, unfortunately 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
1

if you are using the zsh like me:

for f in * ; do mv -- "$f" "${f:u}" ; done
0

If only renaming files/dirs is all you want, then you can use rnm :

rnm -rs '/./\C/g' -fo -dp -1 *

Explanation:

  1. -rs : replace string. /./\C/g replaces all match of . (regex) to it's uppercase.
  2. -fo : file only mode
  3. -dp : depth of directory (-1 means unlimited).

More examples can be found here.

0

Rename all source files to uppercase and stage to git

#!/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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.