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.
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).
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.
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.)
# 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:
>the ':()' shows that we're making a new function named ':'. Everything inside the '{}' is the function body, it makes two recursive calls to the ':' function and puts them into the background with the '&' so that execution continues instead of waiting for them to exit. Then it ends that command with the ';' and executes the function ':'. That makes 2 calls to ':' which each make 2 calls, and so on until ulimit or your hardware capabilities stop it.
Not sure if this is referring to the almost-troll, or the downvoting of said almost-troll.
Without an explanation it's humor for people who know, malicious to those who don't, and at best undermines the trustworthiness of the average comment on this site, which is abnormally high for the internet.
I'm honestly surprised at all the downvoting for this. It's quite literally a powerful command for linux, and let's be honest, you deserve what you get if you type random crap from the internet into your computer. People who have seen it get a chuckle, people who haven't enhance their erudition. Everyone wins. Perhaps adding a link explaining it would have been in order, but failing to do so doesn't, in my opinion, justify nuking the comment from space.
Far more useful than a list of things to copy-paste into a terminal that might be useful in very specific situations would be reading a good explanation of how to use the shell. What the subshells, piping, and some of the basic commands used here are.
24 comments
[ 2.4 ms ] story [ 65.0 ms ] threadmv somefile -t /somedir
cd !$ # now you are in /somedir
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).
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
http://www.caliban.org/bash/ (Under bash Tips and Tricks)
One other good cd trick:
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:- http://twitter.com/bashtips
- http://twitter.com/climagic
- http://twitter.com/zshtips
And an oldie but a goodie:
- http://cb.vu/unixtoolbox.xhtml
- http://www.commandlinefu.com/commands/browse/sort-by-votes
- http://twitter.com/commandlinefu
- http://twitter.com/commandlinefu3 ( 3+ votes)
- http://twitter.com/commandlinefu10 (10+ votes)
http://www.pixelbeat.org/docs/power_of_the_default.html
http://www.pixelbeat.org/docs/web/access_log/monitoring.html
http://www.pixelbeat.org/fslint/
Explanation from a user on the site:
>the ':()' shows that we're making a new function named ':'. Everything inside the '{}' is the function body, it makes two recursive calls to the ':' function and puts them into the background with the '&' so that execution continues instead of waiting for them to exit. Then it ends that command with the ';' and executes the function ':'. That makes 2 calls to ':' which each make 2 calls, and so on until ulimit or your hardware capabilities stop it.
Comment by stuart 91 weeks and 2 days ago
Without an explanation it's humor for people who know, malicious to those who don't, and at best undermines the trustworthiness of the average comment on this site, which is abnormally high for the internet.