Search code examples
bashunixawkfilenames

How can I convert a whole directory tree into uppercase filenames and directory names with bash/awk or similar?


for matching purposes I would need to convert a whole big filesystem (filenames and directory names) into upper case letters. It would be great if we could create links to the orig filenames where the linkname should be the same as the orig filename but in UPPERCASE. Moreover the dir.-tree for the links should also be the same but in UPPERCASE again.

Have anyone here have an idea how this can be handled? Thanks a lot.

Thanks for the advice below. I could do it now this way,but it is still buggy. And I can not find it fast. Any idear where the bug is?

    #! /bin/bash

    walk () {
        local dir=$1
        cd "$dir" || return
        local f
        for f in .* * ; do
            if [[ $f == . || $f == .. ]] ; then
                continue  # Skip the special ones
            fi

            if [[ $f == *[[:lower:]]* ]] ; then  # Skip if no lowercase
                mkdir -p "$2""/""$dir" && ln -s "$(pwd)""/""$f" "$2""/""$dir""/""${f^^}"
            fi
            if [[ -d "$f" ]] ; then
                walk "$f" "$2"
            fi
        done
        cd ..
    }

    walk "$1" "$2"

Solution

  • You can use a recursive function.

    I use the ^^ parameter expansion so I don't have to shell out to tr, it should be faster.

    #! /bin/bash
    
    walk () {
        local dir=$1
        cd "$dir" || return
        local f
        for f in .* * ; do
            if [[ $f == . || $f == .. ]] ; then
                continue  # Skip the special ones
            fi
    
            if [[ $f == *[[:lower:]]* ]] ; then  # Skip if no lowercase
                ln -s "$f" "${f^^}"
            fi
            if [[ -d "$f" ]] ; then
                walk "$f"
            fi
        done
        cd ..
    }
    
    walk "$1"
    

    You'll probably get some errors, for example when FILE and file exist in the same directory, or if the directory can't be entered.