Backtracking with bash
I was working with linux quite a bit today, and frequently changing between directories, when I wondered if there was a way to go back to the directory I was in previously.
Turns out there is a way:
cd ~-So if I was doing something like this:
[pete@bigred /]$ cd /etc [pete@bigred etc]$ cd /usr/local [pete@bigred local]$ cd ~- [pete@bigred etc]$ pwd /etcIf you want to create a command so you don't have to type
~- you can create an alias:
alias cdb='cd ~-'This
~- thing works great if you only need to go back one directory, but what if you wanted to go back two directories. Continuing the last code sample:
[pete@bigred etc]$ cd ~- [pete@bigred local]$ cd ~- [pete@bigred etc]$ pwd /etcWe are back to
/etc and not / our starting point. What I want is something that keeps a history of the directories I've been to.
It turns out that the Bash (the "Bourne again shell") has a directory stack builtin. Three command line tools for manipulating the stack are avaliable dirs, pushd, and popd. More info about the directory stack in bash here.
If we pushd a directory onto the directory stack, we can retreive the top of the stack using dirs +1. I tried setting up some aliases to get it to work the way I wanted:
alias cdd='pushd' alias cdb='cd `dirs +1`'Those worked a bit, but I ran into a lot of problems, especially when in the home directory. Also when you run pushd, popd, or dirs it always prints the contents of the stack, I don't know how to suppress that. So I figured I would post it here, and see if anyone can come up with a solution, or if anyone knows of a better way of going about this.
Isn't it funny how software developers will spend hours of time trying to save a few seconds of their future time.
Trackbacks
Trackback Address: 66/A132A1E502B1F3B7C329191D6E9FBE4C
Comments
On 01/23/2004 at 1:19:30 AM EST John wrote:
1
cd - works. I don't think you need cd ~-.
On 04/24/2004 at 4:50:56 AM EDT Pritam wrote:
2
You can suppress the output redirecting the output to /dev/null e.g. pushd <dir-name> >> /dev/null popd >> /dev/null
On 07/11/2007 at 2:13:43 PM EDT Freddy wrote:
3
You can use small script from http://fvue.nl/cdots which defines 1-7 dots commands:
$/usr/local/share> ... sh[Tab]
$/usr/share>
On 02/21/2008 at 11:55:39 AM EST Simetrical wrote:
4
I was Googling for this and ended up writing these:
function cd { if [ -n "$1" ]; then pushd $1 > /dev/null else pushd $HOME > /dev/null fi } alias bd='popd > /dev/null'
This transparently replaces the cd bash built-in. Note that it doesn't support the -L or -P options, but I've never heard of anyone using those, and anyway they'd be pretty easy to do. Of course, you can use a different name if you don't want to overwrite the default functionality. You do have to use a function for cd to get the redirection right, AFAIK, an alias won't cut it (since bash doesn't replace parameters in aliases).
Post a Comment
Recent Entries
- Cache Template in Request Setting Explained
- What Version of Java is ColdFusion Using?
- ColdFusion 9 Performance Brief from Adobe
- Request Filtering in IIS 7 Howto
- J2EE Session Cookies on ColdFusion / JRun
- Hands on ColdFusion Security Training
- ColdFusion 9 Solr Vulnerability - Are you at Risk?
- FCKEditor Year 2010 Bug for Firefox 3.6 with ColdFusion
$/usr/local/share> ... sh[Tab]
$/usr/share>
function cd { if [ -n "$1" ]; then pushd $1 > /dev/null else pushd $HOME > /dev/null fi } alias bd='popd > /dev/null'
This transparently replaces the cd bash built-in. Note that it doesn't support the -L or -P options, but I've never heard of anyone using those, and anyway they'd be pretty easy to do. Of course, you can use a different name if you don't want to overwrite the default functionality. You do have to use a function for cd to get the redirection right, AFAIK, an alias won't cut it (since bash doesn't replace parameters in aliases).



add to del.icio.us



