BASH script : mketc

I might have unopened mail strewn about my kitchen table, but when it comes to my home directory, I’m a bit of a neat freak. Maybe it’s because I’ve symlinked my Desktop directory to my $HOME:


cd
rm -rf Desktop
ln -s . Desktop

and I can’t stand to have things littered about my desktop, but things have got to be organized in an understandable hierarchy or I start to get a bit twitchy.

Another thing that bugs me is those pain-in-the-neck but vitally important dot-files that *nix users have come to know and love. Since I’ve got my ~ organized with the standard *nix directories (~/var, ~/bin/, ~/src, …) there’s obviously got to be a ~/etc, right? …and what better place than that to put one’s configuration files? So, in comes mketc:


#~/bin/bash

# mketc - creates a ~/etc directory in and populates
#       it with links to all of your ~ dot-files

# first we gotta make the dir, if it isn't already there
if [ ! -d $HOME/etc ] ; then
    mkdir -p $HOME/etc
fi

# chdir to the new etc directory
cd $HOME/etc

# for each dotfile in ~
for file in `ls -d $HOME/.[a-zA-Z0-9]*`; do
    etcfile=$HOME/etc/${file:${#HOME}+2}

    # if it's a directory, we must symlink it
    if [ -d $file ] && [ ! -e $etcfile ] ; then
      ln -s $file $etcfile
    # if it's a regular file, we can hard-link it
    elif [ -f $file ] && [ ! -e $etcfile ] ; then
      ln $file $etcfile
    fi
done

Essentially what this does is create a ~/etc directory (if it doesn’t exist already) and populates it with links to all of the dot-files in your $HOME directory (minus the dot, of course).

An important thing to note about this script is that it hard-links regular files, and symlinks directories.

“Why not just hard-link everything?”

you ask? Good luck trying to hard-link a directory without superuser priveleges (or even with root!).

“Well, then why not symlink everything?”

The simple answer to that is differentiation when using a color-enabled terminal. Granted, the color scheme won’t adhere to what one is used to (since all of the symlinked directories will be colored as symlinks, as they should be), but at least you’ll be able to tell what’s a file and what’s a directory. Also, any regular file type/permissions colors that you happen to have in place will still be available.

Enjoy!

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s