How to access the history on the fly in Unix?

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?

Asked By: Lazer

||

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.

Answered By: jmb

It’s as simple as Alt + .

$ mkdir lasdfjalsdkjf
$ cd 

Alt + .

$ cd lasdfjalsdkjf
Answered By: Umang

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.

Answered By: Banjer

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:~$
Answered By: lcipriani

this has always worked for me:

mkdir thisismyfolder
cd $_
Answered By: Sandy

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.

Answered By: frabjous

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.

Answered By: Elitmiar

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

Answered By: jmanning2k
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.