Linux rename files to uppercase

2020-02-02 08:57发布

问题:

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?

回答1:

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


回答2:

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.



回答3:

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

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 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:

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


回答5:

rename

Probably the easiest way for renaming multiple files is using 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

  • Howto: Linux Rename Multiple Files At a Shell Prompt – nixCraft
  • More info about y/.


回答6:

Using tr:

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

OUTPUT:

X00000.jpg


回答7:

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.