Originally Posted By: peter
Quote:
# capitalize first letter of all items in current directory

But that's not quite what Archeon asked for: the idea was to capitalise the first letter of every word in each filename. Which is harder

Oh dear, you are correct (as always). smile

Here's a program to do what what was asked:
Code:
#!/bin/sh
# capitalize first letter of all items in current directory:
ls -1 | awk '
        ## This script gets run once for each line of output from the "ls -1" command.
        {
                space = " "             ## a space character
                dquote = "\""           ## a double-quote character
                backslash = "\\"        ## a backslash character

                ## Each line of input ($0) is a new file name to convert:
                old_name = $0

                ## Insert backslashes in front of any " characters inside the file name,
                ## so that they are preserved when we later invoke system() on it:
                gsub(dquote, backslash dquote, old_name)

                ## Copy old_name to new_name, and capitalize first letters of all words:
                in_word = 0
                new_name = ""
                for (i = 1; i <= length(old_name); ++i) {
                        tail = substr(old_name,i)
                        c = substr(tail,1,1)
                        if (tail != ".mp3" && tail != ".avi") {
                                if (c !~ "[a-zA-Z]") {
                                        in_word = 0
                                } else if (in_word == 0) {
                                        in_word = 1
                                        c = toupper(c)
                                }
                        }
                        new_name = new_name c
                }

                ## If new_name differs from old_name, then rename the file:
                if (new_name != old_name) {
                        ## Issue a command to rename the file:
                        rename = "/bin/mv"
                        print  rename space dquote old_name dquote space dquote new_name dquote
                        system(rename space dquote old_name dquote space dquote new_name dquote)
                }
        }'


EDIT: updated to leave the ".avi" or ".mp3" extension as-is.


Edited by mlord (01/04/2008 13:35)