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.
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.
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.
# 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.