For example, if I do a

mkdir thisismyfolder912

I remember there is some easier way to switch to thisismyfolder912 than having to do a

cd thisismyfolder912

What is that way and how does it work? Also, what are the other ways I can use this?

  • 1
    Use TAB-completion for commands and directories not in your recent history list. cd this<TAB>
    – invert
    Aug 26, 2010 at 13:37

8 Answers 8


Are you talking about classic history expansion, or Readline processing? cd !$ on the next input line will substitute in the last argument of the previous line, or M-. or M-_ will yank it using Readline.

  • 1
    Post on another question that discusses Alt+. Aug 25, 2010 at 15:36
  • I was looking for cd !$. "Classic history expansion or Readline processing" I don't exactly understand what you mean...
    – Lazer
    Aug 25, 2010 at 15:37
  • 2
    @Lazer !$ is a history expansion; the shell processes it as it parses your command. M-. is a readline keystroke; readline is the library that handles each key you press, and it sees that you pressed M-. and types in the last argument of the last command for you automatically Aug 25, 2010 at 17:00

If your question is about accessing command history, then try this well-named command


You can also try Ctrl + r, and start typing a command you're trying to remember that you've recently typed.

(reverse-i-search)`cd ': cd mydir/data/

Hit ESC to select the command or exit. This works for me on SuSE at least; not sure about other distros.

  • 5
    WHOA! This is actually going to change my life. Screw you history | grep ...!
    – Oli
    Aug 25, 2010 at 21:04
  • This is not just SuSE-related, I think it is a Bash feature :)
    – sakisk
    Mar 17, 2011 at 20:55
  • I just read about using vi to manage your command line. Looks super cool. using vi to edit shell commands
    – Banjer
    Jul 14, 2012 at 14:31

It's as simple as Alt + .

$ mkdir lasdfjalsdkjf
$ cd 

Alt + .

$ cd lasdfjalsdkjf
  • How did you get the fancy box around Alt? I didn't see it in the help guide.
    – Banjer
    Aug 25, 2010 at 16:51
  • 2
    @Banjer: use <kbd></kbd> tags to surround the key.
    – Lazer
    Aug 25, 2010 at 17:28
  • @Banjer <kbd> Aug 25, 2010 at 17:30
  • Also, this is the same as jmb's answer Aug 25, 2010 at 17:34

this has always worked for me:

mkdir thisismyfolder
cd $_
  • Watch out for this, though: $! expands to the last word you actually typed, $_ expands to the last word after expansion - so you might echo /tmp/tmpfile* and see a bunch of files you want to remove, but then rm $_ will only remove one of them.
    – jmb
    Mar 13, 2014 at 12:40

Picking up a tip from another thread, if you put:

bind '"\e[A"':history-search-backward
bind '"\e[B"':history-search-forward

in your .bashrc then, you can start typing something from your history, and then press the up arrow, and then rather than going through your history item by item, it'll skip right to previous entries that begin with what you've already typed.

I guess this doesn't help much with the particular example given in the question, but it is one thing that helps me access history on the fly.


If you use bash i suggest pushd and popd. You can create a stack of directory and browse it rapidly. See this example:

PWD:~$ pushd /opt/google/chrome/resources/
/opt/google/chrome/resources ~
PWD:/opt/google/chrome/resources$ pushd /etc/cron.daily/
/etc/cron.daily /opt/google/chrome/resources ~
PWD:/etc/cron.daily$ pushd /opt/pac/lib/method/
/opt/pac/lib/method /etc/cron.daily /opt/google/chrome/resources ~
PWD:/opt/pac/lib/method$ popd
/etc/cron.daily /opt/google/chrome/resources ~
PWD:/etc/cron.daily$ popd
/opt/google/chrome/resources ~
PWD:/opt/google/chrome/resources$ popd
  • If you use an abbreviated prompt it makes posts more readable. (I'm referring to removing the user and hostnames. I understand that showing the PWD instead of just $ is useful when demonstrating pushd, etc.) Aug 25, 2010 at 22:49

On a related note, I recommend using histverify in bash. Put this in your ~/.bashrc:

shopt -s histverify

This will cause bash to print out the command after expanding !$ or other history functions, and give you a chance to look at it before hitting enter again to actually run it. For me, the sanity check is worth the occasional extra key press. Want to make sure I'm running the cd foo command, not the rm -rf foo one...

I frequently use the Ctrl-R approach, as well as Alt-. (which is a good fit for the scenario you describe). I'll use !$ on occasion.

These are very useful general purpose techniques.

But to address your specific question:

Making a directory and cd'ing directly into it is such a common combination that it is useful to have a function to wrap it up..

function mcd {
  local newdir='_mcd_command_failed_'
  if [ -d "$1" ]; then   # Dir exists, mention that
    echo "$1 exists..."
    if [ -n "$2" ]; then   # We've specified a mode
        command mkdir -p -m $2 "$1" && newdir = "$1"
    else                   # Plain old mkdir
        command mkdir -p "$1" && newdir="$1"
  builtin cd "$newdir"   # No matter what, cd into it

Usage: mcd thisismyfolder


If your shell uses readline (which is the case for bash) you can do something like pressing ATL+. together?

From the GNU Readline documentation:

yank-last-arg (M-. or M-_)

Insert last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .