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 a folder of files and I want to change the case of one of their characters at the 5th position.

From this:

ABC-xyz
DEF-xyz
GHI-xys

To this:

ABC-Xyz
DEF-Xyz
GHI-Xys

You'll notice the X has been converted to uppercase.

Any ideas how I'd do this in Bash?

share|improve this question
    
Are the filenames actually in that pattern, so the letter that needs to change the case is after a -? And do you want just bash, or are other common *nix utilities like sed etc. fine too? –  evilsoup May 27 '13 at 22:24
    
Any common utilities which can be invoked from bash would be fine. And yes the files, in this case, are in that pattern! –  WackGet May 28 '13 at 0:16
add comment

1 Answer

up vote 4 down vote accepted

Pure bash example:

#!/usr/bin/env bash

for f in *; do
  g="${f::4}"  ##Split the first four characters
  h="${f:4:1}" ##just the fifth character (starts counting at 0)
  i="${f:5}"   ## character 6+ (again, counting from 0)
  mv -- "$f" "$g${h^^}$i"
    ##At the end, put the strings back together
    ##but make $h (character 5) uppercase
done
exit 0

In reality, I would probably use perl-rename (called rename in the Ubuntu repos; I know that in some others it goes by prename):

rename 's/(.{4})(.)/$1\u$2/' *
share|improve this answer
    
The rename option looks very elegant. Thank you. –  WackGet May 28 '13 at 0:17
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.