Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
Use TAB-completion for commands and directories not in your recent history list. cd this<TAB> –  invert Aug 26 '10 at 13:37

8 Answers 8

up vote 11 down vote accepted

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.

share|improve this answer
1  
Post on another question that discusses Alt+. –  Michael Mrozek Aug 25 '10 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 '10 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 –  Michael Mrozek Aug 25 '10 at 17:00
    
@Michael Mrozek: thanks! –  Lazer Aug 26 '10 at 15:43

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.

share|improve this answer

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..."
    newdir="$1"
  else
    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"
    fi
  fi
  builtin cd "$newdir"   # No matter what, cd into it
}

Usage: mcd thisismyfolder

share|improve this answer

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
~
PWD:~$
share|improve this answer
    
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.) –  Dennis Williamson Aug 25 '10 at 22:49
    
I'have fixed, thank you. –  lcipriani Aug 27 '10 at 7:14

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.

share|improve this answer

this has always worked for me:

mkdir thisismyfolder
cd $_
share|improve this answer
    
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 at 12:40

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

history 

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.

share|improve this answer
4  
WHOA! This is actually going to change my life. Screw you history | grep ...! –  Oli Aug 25 '10 at 21:04
    
This is not just SuSE-related, I think it is a Bash feature :) –  faif Mar 17 '11 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 '12 at 14:31

It's as simple as Alt + .

$ mkdir lasdfjalsdkjf
$ cd 

Alt + .

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.