Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have about 2k of file that are currently in lower case like:

file_one.cfr
file_two.cfr
....

I am searching for a fast way to rename them to upper case so they would be like;

FILE_ONE.cfr
FILE_TWO.cfr
....

If I use from my shell;

for i in *; do mv $i `echo $i | tr [:lower:] [:upper:]`; done

I can get all file and the file extensions to upper case.

But the extension should remain in lowercase, so my approach does not work.

Any programming language is welcome.

share|improve this question

migrated from stackoverflow.com Apr 11 '10 at 23:26

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers 5

up vote 2 down vote accepted

You can always use the free Bulk Rename utility.

share|improve this answer
    
Thank you @Oded that is what I need. –  Adnan Apr 11 '10 at 13:59
    
That seems to be for Windows. –  Dennis Williamson Apr 11 '10 at 23:45
    
I don't understand, judging from the tags of the question, the OP is asking for a linux shell command, and you reply proposing a windows GUI application? –  mrucci Apr 15 '10 at 12:41
add comment

In any shell:

for i in *.cfr 
do 
    mv "$i" "$(echo ${i%.cfr} | tr [:lower:] [:upper:]).cfr"; 
done

It is based on your proposed solution but uses the parameter expansion ${parameter%word} to strip the extension from the filename.

share|improve this answer
    
You should probably quote the second expansion of i to preserve IFS characters other than SPC (e.g. TAB and LF). Also, you might quote the character classes for shells that are configured to delete or produce an error for non-matching globs. –  Chris Johnsen May 18 '10 at 5:07
add comment

if you have bash 4.0

#!/bin/bash

for file in file_*cfr
do
  front=${file%.cfr}
  newfile=${front^^}.cfr
  echo mv "$file" "$newfile"
done

or you can use just awk

awk '
{
  f=FILENAME
  gsub(/\.cfr$/,"",f)
  newfile=toupper(f)".cfr"
  cmd="mv \047"FILENAME"\047 \047"newfile"\047"
  # system(cmd) #uncomment to use
}
' file_*cfr
share|improve this answer
    
you would need perhaps | bash at the very end –  Vlad Apr 11 '10 at 14:29
add comment

You may already have a Perl script on your system called rename or prename.

rename 's/([^.]*)/\U$1/' *.cfr

Or here's a version of Vlad's script that eliminates the need for grep and sed (it will work in Bash versions earlier than version 4 since it uses tr instead of the ^^ operator):

#!/bin/bash

for f in *.cfr
do
    if [[ "$f" == "${f//.}" ]]; then continue; fi
    name="${f%.*}"
    ext="${f##*.}"
    newfullname="$(<<< "$name" tr [:lower:] [:upper:])$ext"
    [[ "$f" == "$newfullname" ]] || mv "$f" "$newfullname"
done
share|improve this answer
add comment
#!/bin/bash

for f in *
do
    <<< $f grep -q "\." || continue
    name=$(<<<$f sed 's/\.[^\.]*$//')
    ext=$(<<< $f sed 's/^.*\(\.[^\.]*\)$/\1/')
    newfullname=$(<<< $name tr [:lower:] [:upper:])$ext
    [ "$f" == "$newfullname" ] || mv "$f" "$newfullname"
done
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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