Mark Needham

Thoughts on Software Development

Browsing around the Unix shell more easily

with 9 comments

Following on from my post about getting the pwd to display on the bash prompt all the time I have learnt a couple of other tricks to make the shell experience more productive.

Aliases are the first new concept I came across and several members of my current team and I now have these setup.

We are primarily using them to provide a shortcut command to get to various locations in the file system. For example I have the following 'work' alias in my ~/.bash_profile file:

alias work='cd ~/path/to/my/current/project'

I can then go to the bash prompt and type 'work' and it navigates straight there. You can put as many different aliases as you want in there, just don't forget to execute the following command after adding new ones to get them reflected in the current shell:

. ~/.bash_profile

A very simple idea but one that helps save so many keystrokes for me every day.

Another couple of cool commands I recently discovered are pushd and popd

They help provide a stack to store directories on, which I have found particularly useful when browsing between distant directories.

For example suppose I am in the directory '/Users/mneedham/Desktop/Blog/' but I want to go to '/Users/mneedham/Projects/Ruby/path/to/some/code' to take a look at some code.

Before changing to that directory I can execute:

pushd .

This will push the current directory ('/Users/mneedham/Desktop/Blog/') onto the stack. Then once I'm done I just need to run:

popd

I'm back to '/Users/mneedham/Desktop/Blog/' with a lot less typing.

Running the following command shows a list of the directories currently on the stack:

dirs

I love navigating with the shell so if you've get any other useful tips please share them!

Written by Mark Needham

October 15th, 2008 at 10:31 pm

Posted in Shell Scripting

Tagged with

9 Responses to 'Browsing around the Unix shell more easily'

Subscribe to comments with RSS or TrackBack to 'Browsing around the Unix shell more easily'.

  1. [...] Browsing around the Unix shell more easily By Mark Needham I'm back to '/Users/mneedham/Desktop/Blog/' with a lot less typing. Running the following command shows a list of the directories currently on the stack:. dirs. I love navigating with the shell so if you've get any other useful tips … Mark Needham – http://www.markhneedham.com/blog [...]

  2. An alternative to aliases is BASH's "cdable_vars" feature. Enable this with the shopt command in BASH. This allows you to create variables that contain paths (e.g. mydir=/home/rick), which you can then cd into (e.g. cd mydir). Yet another way to do the same thing. I enjoy reading your posts!

    Rick Umali

    15 Oct 08 at 11:37 pm

  3. One of my favorites which I picked up from a co-worker is the recall facility in a Unix shell.

    Here is a link that explains this and saves keys strokes like no other.

    http://techie-buzz.com/linux-tips/how-to-recall-typed-commands-in-linux.html

    sud

    16 Oct 08 at 12:18 am

  4. Try zsh as an alternative to bash. It has all the features of bash, plus some extras that make life so much easier. For example:

    Extended file globbing. e.g. ** will expand through all the subdirectories, you can do case insensitive matches, OR matches, match files and remove the extension, and so on. So "ls **/*.(java|(#i)xml)" will list all files in the current directory and all subdirectories with the extension java or xml (where xml is case insensitive). I use the ** feature all the time, and hardly ever have to use the 'find' command.

    As well as supporting the search facility that sud mentioned, you can also have a mode where you type the first few characters of a line in your history, then can cycle through the lines that start with those characters. I find this much more natural than searches. I have this bound to the and keys, since that is what I am used to in Vim.

    Smart Completion. zsh not only knows about filenames but also about commands. So for example (if you have enabled it) type "svn " to list all the svn subcommands, or "ls -" to list all the options to ls. You can add your own smart completions for new commands.

    There are lots and lots of other features (I am still learning about them), but IMHO the ** directory expansion alone is reason enough to switch. Zsh is in most linux repositories and cygwin. You can find out more at http://zsh.dotsrc.org/.

    Dave Kirby

    16 Oct 08 at 9:02 am

  5. Damn, the comment system stripped out my text in angle brackets – that should be "svn TAB" and "ls -TAB" in the penultimate paragraph, and CTRL-N and CTRL-P to move through the history.

    Dave Kirby

    16 Oct 08 at 9:09 am

  6. Another quick one if you don't want to bother with popd and pushd is to use "cd -" to go to the last directory you were from. Of course this does not work if you traverse your directory one by one manually most of the time. It can be useful if you jump to other directory faraway by using "cd /xyz/abc/def/fda/kfjd", once you are finish by typicng "cd -" it will bring you back to your original directory.

    Wirianto Djunaidi

    16 Oct 08 at 11:41 am

  7. I use another strategy: I don't try to make it easier to cd, I remove the need by dedicating one terminal window for one task.

    The shade feature of openbox (many other windowmanagers support it, too) helps here, because I can keep most of the terminals shaded, and the cwd is visible in the title bar so I know which terminal to unshade when I need to do any particular task.

    Ville Oikarinen

    21 Oct 08 at 11:30 pm

  8. I have always been a fan of pushd/popd/dirs

    I use the following:

    alias cdh="dirs -v"
    alias cd="pushd"
    alias cd-="popd"
    alias cd–="popd;popd"
    alias cd—="popd;popd;popd"

    alias up2="cd ../.."
    alias up3="cd ../../.."

    I also like to use:
    alias ll="ls -lrt"
    and my all time favourite:
    alias lds="ls-l | grep ^drw"

    Then
    cd ~1 #will take you to the dir at position 1 in the stack
    cd #rotates the stack. NB default cd is to take you to home directory

    Bill Comer

    23 Oct 08 at 1:47 am

  9. Check out http://muness.blogspot.com/2008/06/lazy-bash-cd-aliaes.html for a script that makes creating these aliases easier to create: you only have to specify the parent directory that has all your projects and it creates aliases for all of them.

    Muness Alrubaie

    23 Oct 08 at 8:30 am

Leave a Reply