4 ms·
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 (thi
by bstar77 13y ago
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).
- samstave 13y agoI 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/)
- scoot 13y agopushd /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...
- deleted 13y ago[deleted]
- jbp 13y agoI use pushd/popd/dirs for multiples. I have aliases like p1 for "pushd +1" and so on.
- jlgaddis 13y agocf. pushd and popd
- spenuke 13y agoI 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.
- pbhjpbhj 13y agoYou 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.
- octo_t 13y agofunction take_dir { mkdir -p "$1"; cd "$1" ; }; alias take="take_dir"
- barrkel 13y agoI call my version of this function 'enter'.
- jlgaddis 13y agoI 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
- roryokane 13y agoI use the version at http://onethingwell.org/post/586977440/mkcd-improved 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
- vram22 13y ago$_ 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.
- elipsey 13y agoyou just made my day :)
- minimax 13y ago"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 }
- amboar 13y agoI'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.
- amboar 13y agoHere's the script for ct: http://pastebin.com/M1k1WL88 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.
- wyclif 13y agoThat's weird. "cd" with options was one of the first commands I learned.