scripting essentials: fullpath

One of the things you end up doing in cli world a lot is jacking around with files.

Usually you are doing some junk with files in some (collection of) terminal(s) with a given working directory and referring to files with a local path eg: src/main/webapp/WEB-INF/web.xml

Then… for some reason, you want to manipulate them from another context, maybe a browser or some shells doing stuff in another working directory. In some cases you could change working directories, but is that really what you want?

Kind of breaks the flow, doesn’t it? “Now let me hop over here…”

Why are you hopping? You want to grep src/main/webapp/WEB-INF/web.xml? Just use the fullpath name, eg: /home/jameson/src/devilish/src/main/webapp/WEB-INF/web.xml !

But how? EZ: echo ${PWD}/src/main/webapp/WEB-INF/web.xml

By jamming around the fullname instead of hopping around the filesystem like a bunny rabbit, you will be better able to maintain the separation between the different contexts of your activities.

Ya dig it? If not, try this approach out for a while and see if you can feel the difference.

To make this easier, I prefer to use a bash function like this:

fullpath() { 
    local f
    for f in ${*}; do
        ( 
		 	( 
			 	cd ${f} && echo ${PWD} 
			) || ( 
			 	cd $(dirname ${f} ) && echo ${PWD}/$( basename ${f} ) 
			) 
		) 2> /dev/null
    done
}

This takes care of some annoying corner cases that I can’t remember at the moment… but feel free to shorten it

This tip says: move the world around your mind, not your mind around the world.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: