I often want to change into the directory of a file on my file system, but I'm not sure where the file is.
I search for it like so:
find -type f -name "myfile.txt"
Lets say for the sake of simplicity, that this returns one result, eg.
/some/super/long/path/that/i dont/want/to/type/myfile.txt
I then usually type this:
cd $(dirname $(!!))
To switch into the directory of the file I was searching for….
Is there anyway to put this into a shell script or alias, so I can basically type:
cdlast
and it runs:
cd $(dirname $(!!))
using the shell's HISTORY? I've tried it, and the shell history seems to be missing in bash scripts.
Best Answer
fc -s
runs the previous command again:Or use
eval "$(history -p !!)"
: