¿Cómo puedo volver al último directorio utilizado en un shell de Linux?


0

¿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?


1
$OLDPWDalmacenar (cuando exista) la ruta al antiguo directorio utilizado. Escribir cd -es el equivalente (más rápido) de escribir cd $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 * $OLDPWD etc., etc.
Hastur

@Hastur es pwd cosas comunes? Mientras escribía cd $ NEWPWD resulta igual
BlueBerry - Vignesh4303

You can see OLDPWD as the name of a system variable. When you write 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 write cd $A_Not_set_VARIABLE is like if you write cd 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 :)
Hastur

Tenga en cuenta que "terminal de Linux" no tiene sentido por dos razones: 1) el terminal es en realidad lo que le permite ingresar caracteres y verlos impresos; 2) 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 ;-)
kostix

Respuestas:


4

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 the cd - 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 a cd -. 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 (or cd $owd)

cd is an internal command of all the shells (all, starting from sh).
Underdash and bash it will set the variable PWD, OLDPWD for the present and old working directory. Under csh and tcsh it will set instead cwd and owd.

So with the command cd $OLDPWD in bash or cd $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 and popd removes one from the stack. An advantage respect cd - 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 and help popd for the information about those builtin commands.


Notes:

  1. To write cd $NotAlreadySetVariable is the equivalent to write cd 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.

  2. 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)...

PWD       The logical value of the current working directory.
          This is set by the cd command.

OLDPWD    The previous logical value of the current working directory.  
          This is set by the cd command.

HOME      The  home  directory of the current user; the default argument for  
          the cd builtin command. The value of this variable is also used when   
          performing tilde expansion.

from man tcsh

cd and pushd interpret `-' as the old working directory (equivalent to the shell variable owd). This is not a substitution at all, but an abbreviation recognized by only those commands. Nonetheless, it too can be prevented by quoting.



3

use pushd dir to change directory use popd to change dir back use dirs to view directories stack use pushd to switch current dir with previous

Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.