I would like to construct a short function to do the following. Let's say that I move file 'file.tex' to my documents directory:
mv file.tex ~/Documents
Then, I'd like to cd
to that directory:
cd ~/Documents
I'd like to generalize this to any directory, so that I can do this:
mv file.tex ~/Documents
follow
and have the follow
command read the destination from the previous command, then execute accordingly. For a simple directory, this doesn't save much time, but when working with nested directories, it would be tremendous to be able to just use
mv file.tex ~/Documents/folder1/subfolder1
follow
I thought it would be relatively simple, and that I could do something like this:
follow()
{
place=`history 2 | sed -n '1p;1q' | rev | cut -d ' ' -f1 | rev`
cd $place
}
but this doesn't seem to work. If I echo $place
, I do get the desired string (I'm testing it with ~/Documents
), but the last command returns
No such file or directory
The directory certainly exists. I'm at a loss. Could you help me out?
file.tex
in the original location, symlinks are a very good alternative, since you only have to link once, and then it will always point to the latest version. – Kroltan Aug 15 '16 at 14:12cd
alt+.
to substitute the last token of the previous command. Repeat to go further back in the history of final tokens. (I say token not arg, becausefoo &
grabs&
as the final token.) You can use a numeric argument (with escape-3 alt+. for example). – Peter Cordes Aug 15 '16 at 22:28