Re:Some powerful commands for Linux
by junkbit on Saturday November 27, 2010 @11:05AM
(#1945164)
'cd -' go back to previous directory. Genius! How did I not know this?
[ Parent
| on HN ]
Re:Some powerful commands for Linux
by telemachos on Sunday November 28, 2010 @10:18AM
(#1947463)
If you don't know it, I highly recommend learning about CDPATH as well. A good CDPATH means that directories you work in often are always easy to get to. (Add TAB completion in its latest version and they're even easier to get to.)
http://www.caliban.org/bash/ (Under bash Tips and Tricks)
One other good cd trick:
# Shamelessly stolen from Learning the Bash Shell (3ed), Cameron Newham
# & Bill Rosenblatt
my_cd() {
case "$#" in
0|1)
builtin cd $1
;;
2)
newdir=${PWD//$1/$2}
case "$newdir" in
$PWD)
echo "bash: my_cd: bad substitution" >&2
return 1
;;
*)
builtin cd "$newdir"
;;
esac
;;
*)
echo "bash: my_cd: wrong arg count" 1>&2
return 1
;;
esac
}
The function allows you to do simple search/replace on your current working directory and cd to the replaced version, all in one step. This is a built-in ksh feature, but easy enough to enable in bash. If you have directories with relatively deep nesting and very similar names (e.g. school/2009-2010/compsci1/grades versus school/2010-2011/compsci2/grades), it's a godsend. An example: $ pwd
/Users/telemachus/mmt/2010-2011/compsci2
$ my_cd compsci2 compsci4
$ pwd
/Users/telemachus/mmt/2010-2011/compsci4'cd !$' is another recent discovery for me, as in:
mv somefile -t /somedir
cd !$ # now you are in /somedir
A more general way to do this is with M-. (Esc-.). This fetches the last argument from the previously entered command. Very handy when you need to apply multiple commands to a file or directory.
Repeat this key combo to fetch the last argument from further back in your command history.
I use this all the time (almost as much as tab completion).
even better (if you are using bash, not sure about other shells) and use
shopt -s histverify
then if you type !$<enter> will replace !$ with /somedir but not hit return, so you could then modify it. Then you could do...
mv somefile -t /somedir
vim !$<enter>/somefile
Try adding this to your .inputrc file
$if Bash
Space: magic-space
$endif
Now the history expansion will be done when 'space' is pressed.
Re:Some powerful commands for Linux
by adambyrtek on Sunday November 28, 2010 @06:56AM
(#1947190)
You should consider investing some time to go through a good shell tutorial, maybe even read a book on shells. You will find many more pearls like 'cd -', which is really one of the most basic and well known ones.
Re:Some powerful commands for Linux
by RyanMcGreal on Saturday November 27, 2010 @02:28PM
(#1945650)
It's really handy if you need to jump back and forth between two directories with diverse paths.
For that I usually use pushd.
[chronomex@gir ~]$ mkdir test1 test2
[chronomex@gir ~]$ cd test2
[chronomex@gir ~/test2]$ pwd
/home/chronomex/test2
[chronomex@gir ~/test2]$ pushd ../test1
~/test1 ~/test2
[chronomex@gir ~/test1]$ pwd
/home/chronomex/test1
[chronomex@gir ~/test1]$ pushd
~/test2 ~/test1
[chronomex@gir ~/test2]$ pwd
/home/chronomex/test2
[chronomex@gir ~/test2]$On older systems you had to do a fairly complicated set up for pushd/popd yourself. I did once in along 1998 or so, but never bothered again, since the great majority of the time "cd -" was good enough.