Posts Tagged ‘bash’

cd to git_root

Wednesday, May 6th, 2009

When I’m wailing on a project in bash, I frequently find myself wanting to cd back to the project root. Since we use git, this is the same as the git_root directory. So I wrote a bash function that looped through the directories in the current path and found the one that contained the .git/ directory.

Luckily, I thought to ask on #git if there was an easier way before I posted my script (because no one likes to look like a noob on their own blog). It turns out there’s a one liner. I’ve posted it here, with a little padding so that I can feel like I made a contribution.


function gr {
    ## If the current working directory is inside of 
    ## a git repository, this function will change
    ## it to the git root (ie, the directory that 
    ## contains the .git/ directory), and then print
    ## the new directory.    
    git branch > /dev/null 2>&1 || return 1
    cd "$(git rev-parse --show-cdup)".
    pwd
}

bash-fu: You forgot to sudo

Wednesday, August 6th, 2008

One of the more routine annoyances of a long day in the terminal is banging out some long command that happens to operate on a file your current user doesn’t have access to.

www@webserver:~$ rm -fr /usr/lib/python2.5/site-packages/buildbot/buildbot.png<br/><br/> rm: cannot remove `/usr/lib/python2.5/site-packages/buildbot/buildbot.png': Permission denied

You declined to sudo the command because you you forgot, were feeling cautious, or were unaware of the permissions bits of the file(s) in question. Now you have to type it again, or switch gears and start up some cursor navigation to prepend your sudo.

Next time, as an easy to use alternative, try:

sudo !!

In bash, !! expands to the last run command. sudo !! sudo’s the last command. That was easy.

Subscribe:

Add to Google
RSS
Try ExpanDrive

If you’ve heard of SSH then you need ExpanDrive.