¿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/Desktop
directorio (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 cd
comando para volver al directorio anterior?
$OLDPWD
almacenar (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$OLDPWD
cuándo desea mover, por ejemplo, algunos archivos del directorio actual al antiguo,mv * $OLDPWD
etc., etc.echo $OLDPWD
it 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).pwd
is 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_VARIABLE
is like if you writecd
that 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 :)cd
es 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
)cd
is an internal command of all the shells (all, starting fromsh
).Under
dash
andbash
it will set the variablePWD
,OLDPWD
for the present and old working directory. Undercsh
andtcsh
it will set insteadcwd
andowd
.So with the command
cd $OLDPWD
in bash orcd $owd
in 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
pushd
adds a directory to the stack andpopd
removes 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 pushd
andhelp popd
for the information about those builtin commands.Notes:
To write
cd $NotAlreadySetVariable
is the equivalent to writecd
with 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
,$owd
variable 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 bash
that in the definition of PWD is used current working directory(cwd) and not something like present working directory (PWD)...from
man tcsh
fuente
i figured out using
cd
commandwill redirect back to the last used directory
fuente
use
pushd dir
to change directory usepopd
to change dir back usedirs
to view directories stack usepushd
to switch current dir with previousfuente