I decided to restructure the folder I keep code in (~/code, natch) - taking my cue from how Go does it - so that the folder structure represents where code has come from.

As with all things, moving a couple of hundred folders by hand seemed far too daunting so I wrote a bash script to do it.

This script enters each subdirectory within the current directory and, if it has a git remote, moves it to a folder that represents the git remote’s path.

For example, if I had a folder called scripts that had a git remote of git@github.com/stilvoid/utils.git, this script will move the folder to github.com/stilvoid/utils.


# Target directory for renamed folders

for i in $(find ./ -maxdepth 1 -mindepth 1 -type d); do
    cd "$i"
    folder="$(git remote -v 2>/dev/null | head -n 1 | awk '{print $2}' | sed -e 's/^.*:\/\///' | sed -e 's/:/\//' | sed -e 's/^.*@//' | sed -e 's/\.git$//')"
    cd ..

    if [ -n "$folder" ]; then
        mkdir -p "$BASE/$(dirname $folder)"
        mv "$i" "$BASE/$folder"

Yes it’s horrid but it did today’s job ;)