Jay
Jay

Reputation: 2129

Linux rename files to uppercase

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?

Upvotes: 27

Views: 52569

Answers (9)

Pavel Skipenes
Pavel Skipenes

Reputation: 379

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

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263577

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.

Upvotes: 16

Pablo Bianchi
Pablo Bianchi

Reputation: 2058

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

Upvotes: 15

Cem Yıldız
Cem Yıldız

Reputation: 11

if you are using the zsh like me:

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

Upvotes: 1

anubhava
anubhava

Reputation: 785856

Using tr:

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

OUTPUT:

X00000.jpg

Upvotes: 3

Jahid
Jahid

Reputation: 22438

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.

Upvotes: 0

Wiley
Wiley

Reputation: 526

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

Upvotes: 8

damienfrancois
damienfrancois

Reputation: 59290

for f in * ; do mv -- "$f" "$(tr [:lower:] [:upper:] <<< "$f")" ; done

Upvotes: 31

pts
pts

Reputation: 87381

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.

Upvotes: 20

Related Questions