¿Cómo puedo volver al último directorio utilizado en un shell de Linux?
Por ejemplo: abro un nuevo shell (o un registro en un shell de consola diferente) y escribo:
root@vignesh : cd /root/Desktop
Esto me dirigirá al /root/Desktopdirectorio (si tengo acceso a él)
root@vignesh:~/Desktop#
¿Cómo puedo volver ahora al directorio anterior?
¿Hay algún comando para volver al último directorio que utilicé?
¿Necesitaba usar nuevamente el cdcomando para volver al directorio anterior?

$OLDPWDalmacenar (cuando exista) la ruta al antiguo directorio utilizado. Escribircd -es el equivalente (más rápido) de escribircd $OLDPWD. Puede darse cuenta de que puede usar más ampliamente$OLDPWDcuándo desea mover, por ejemplo, algunos archivos del directorio actual al antiguo,mv * $OLDPWDetc., etc.echo $OLDPWDit prints on the screen the value of that variable, in other words the old path in which you were. NEWPWD is an appropriate name for a variable but is not used by the system (I mean bash).pwdis both an internal command of bash and its duplicate executable in/bin/pwd, that prints the name of the current working directory. When you writecd $A_Not_set_VARIABLEis like if you writecdthat bring you to your home and set the OLDPWD to the old path: it seems it didn't do anything... but it is not so :)cdes un comando proporcionado por el llamado "shell". El shell es lo que te saluda con un aviso, proporciona la ejecución de comandos, el historial de comandos, etc. Entiendo que no está familiarizado con estos conceptos, así que quería alimentar su impulso de investigación ;-)Respuestas:
cd -cd -will bring you to the previously used directory (if there is one) or it will generate an error.If a previously used directory exists it will change there updating the value of the current working directory and of the previous one, returning a successful exit status (
0).Otherwise it will print an error message and it will return an exit status (
1).Do your own check with
cd -; echo $?.The exit status will become important when you will use it in a script.
An exit status different from 0 can stop the execution of a whole script (if there is a
set -e) or, worst, can lead to an exit point of thecd -command, different from the one you were thinking when you wrote the script and to execute commands in the wrong directory: imagine that you start in dir0; after you change to dir1 then you fail to change to dir2. Now you execute acd -. You think to be in dir1 but instead you are in dir0... and from here it all in the hands of the Fate.cd $OLDPWD(orcd $owd)cdis an internal command of all the shells (all, starting fromsh).Under
dashandbashit will set the variablePWD,OLDPWDfor the present and old working directory. Undercshandtcshit will set insteadcwdandowd.So with the command
cd $OLDPWDin bash orcd $owdin tcsh you will be brought to the old working directory if it exists, or to your home directory if this variable is not set.The exit code will be always
0, if you have access to your home directory(1).pushd newdir ... popd
pushdadds a directory to the stack andpopdremoves one from the stack. An advantage respectcd -is that you will choose when to come back to the signed directory, and you will not be forced to come back to the last one. Another advantage is that you can pile a number of directories and decide to which to jump.help pushdandhelp popdfor the information about those builtin commands.Notes:
To write
cd $NotAlreadySetVariableis the equivalent to writecdwith no parameters at all that will bring you to your home directory. To be precise it will bring you to the directory inside of$HOME(for bash,dash...) or$home(for csh,tcsh...). If this directory doesn't exist or is not accessible you will receive an error. If$HOME(or$home) is empty you will remain in the present directory, no error will be generated, and the$OLDPWD(or$owd) value will be set to the present directory.The
$OLDPWD,$owdvariable can be useful when you want to use the previous directory as parameter for a command. E.g. You want to move, all files from the present directory to the old one:mv * $OLDPWD.Trivial
To be noted from
man bashthat in the definition of PWD is used current working directory(cwd) and not something like present working directory (PWD)...from
man tcshfuente
i figured out using
cdcommandwill redirect back to the last used directory
fuente
use
pushd dirto change directory usepopdto change dir back usedirsto view directories stack usepushdto switch current dir with previousfuente