I have a folder of files and I want to change the case of one of their characters at the 5th position.

From this:


To this:


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

Any ideas how I'd do this in Bash?

  • 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

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
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/' *
  • The rename option looks very elegant. Thank you. – WackGet May 28 '13 at 0:17

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.