3

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.

2

You can always use the free Bulk Rename utility.

2
  • That seems to be for Windows. Apr 11 '10 at 23:45
  • 1
    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
4

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.

1
  • 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. May 18 '10 at 5:07
3

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
2
  • Why not rename 'y/a-z/A-Z/' * ?
    – Pablo A
    Aug 11 '17 at 18:40
  • @PabloBianchi: The OP wanted to uppercase only the part before the dot. Aug 23 '17 at 13:10
2

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
1
  • you would need perhaps | bash at the very end
    – Vlad
    Apr 11 '10 at 14:29
1
#!/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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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