Hacker News new | past | comments | ask | show | jobs | submit login

I find it interesting that very few people know about the "cd -" command (goto previous directory). I find it invaluable, but never see anyone else use it (this tutorial included).



"cd -" and also using "cd" with no arguments to get back to $HOME are great tips. I also have a bash function called "up" to get back N directories up the hierarchy so I can do "up 5" which is like "cd ../../../../.." but much easier to type.

    up () {
        if [[ $# -eq 1 && "$1" -gt 0 ]] ; then
            local i d
            for (( i = 0; i < $1; i++ )) ; do d="../$d" ; done
            cd $d
        else
            echo "Usage: up N"
        fi
    }


I'm on my phone at the moment, but I did something similar except avoided the counting. I have a shell function called `ct` for "climb tree" where you specify the name of the directory you want to climb to; calling it will place you in the first directory above cwd whose name matches the argument and in addition I wrote a bash completion module for it so I only have to type minimal characters. I'll post the source for both shortly.


Here's the script for ct:

http://pastebin.com/M1k1WL88

Looks like I wrote the tab-completion script on my work machine. I don't have access to it right now, but if I remember when I do have access I'll pastebin that as well.


This is awesome! You are awesome! :-)


Here's the tab completion script:

http://pastebin.com/mR8y1Rks

Note it's the only tab completion script I've written, and it's probably a hack :P


Thanks for following up. I think I'm going to play with this later this week.


No worries. Just throw them both in your .bashrc and you should be good to go.


I like this concept. My solution has been to use '..' '...' and '....' to go up 1,2,and 3 directories.


oh-my-zsh does this out of the box.


Your function called "up" reminds me of long-ago-used Novell Netware version, in which, IIRC, you could do:

cd .. # to go one level up

cd ... # to go two levels up (note: 3 dots, not 4, makes sense when you think of "cd ." and "cd ..")

# and so on.


I didn't know this one, and glad I do now! Is it possible that the other one I wish existed is already commonplace? I'd like to have an arg that cd's to a directory automatically after mkdir'ing it... or mv'ing a file to a directory.

Something like:

mkdir foo -go and mv foo ~/bar -go

... Probably simple enough to script, but I guess I'm too lazy at the moment.


You could have scripted it faster than doing that comment ...

Or, you could just

    bash$ mkdir -p /some/dir/to/make
    bash$ cd !$
Bang (!) is a history lookup with lots of variation in use. It probably works in other *nix shells too.

Look under "event designators" in "man bash" for more info.


I typically use:

    $ mkdir foo
    $ cd !$
Not quite what you're asking for but it can certainly save some keystrokes -- especially if you're doing something like:

    $ mkdir -p foo/bar/baz/whatever


I use the version at http://onethingwell.org/post/586977440/mkcd-improved, called `mkcd`.

  # mkdir, cd into it
  mkcd () {
      mkdir -p "$*"
      cd "$*"
  }
Just copy and paste that at the end of your `.bashrc` or `.zshrc` file. You may also want to include this comment above it so you know why it’s written like that:

  # from http://onethingwell.org/post/586977440/mkcd-improved


$_ in bash means the last argument of the previous command. So:

mkdir some/long/path/to/a/dir cd $_

mv foo some/long/path/bar cd $_

both work for what you want, and the extra keystrokes are so few that it hardly matters. The $_ is not specific to those two commands, so works for others as well, and is typically very useful because command sequences often tend to need the last argument of the previous command in the next command; un-tar-ing a .tar file and then going to the extracted directory is another example.


function take_dir { mkdir -p "$1"; cd "$1" ; }; alias take="take_dir"


I call my version of this function 'enter'.


I use it a lot. I wish i could add multiples to go backfurther....

i.e.:

cd /var/log

cd /etc/ssh

cd /home/samstave/Downloads

cd -- (takes me back to /var/log/)


    pushd /var/log
    pushd  /etc/ssh
    pushd /home/samstave/Downloads

    popd;
    !! (takes me back to /var/log/)
You could alias cd to pushd and cd- to popd...


I use pushd/popd/dirs for multiples. I have aliases like p1 for "pushd +1" and so on.


cf. pushd and popd


That's weird. "cd" with options was one of the first commands I learned.


you just made my day :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: