Linux return to previous directory

How to change directory in Linux terminal

M y Dell Laptop came preinstalled with Ubuntu Linux, and I am a new Linux desktop user. How do I change directories in the Linux terminal?

Introduction – On Linux the cd command allows you to change directories when using the terminal application. This page shows how to change directory in Linux terminal using the cd command.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux terminal
Est. reading time 3 minutes

How to change directory in Linux terminal

  1. To return to the home directory immediately, use cd

OR cd

  • To change into the root directory of Linux file system, use cd / .
  • To go into the root user directory, run cd /root/ as root user.
  • To navigate up one directory level up, use cd ..
  • To go back to the previous directory, use cd —
  • Let us see all examples and usage for terminal in details.

    How to use the Linux command line to change directory or folder

    The directory in which the user is currently working is called the current working directory (CDW).

    How to print the current working directory in Linux

    To display the name of the current/working directory, type the following pwd command:
    pwd

    cd command in Linux termianl

    The syntax is:
    cd
    cd ..
    cd /path/to/dir
    When cd command used without stipulating any directory name, cd command returns to the home directory. Let us change the directory to /usr/sbin/, run:
    cd /usr/sbin/
    Verify it:
    pwd
    Want to list the files in the /usb/sbin/ directory? Try the ls command:
    ls
    ls -l
    Let us go back to user’s home directory, run:
    cd
    Again verify it:
    pwd

    • No ads and tracking
    • In-depth guides for developers and sysadmins at Opensourceflare✨
    • Join my Patreon to support independent content creators and start reading latest guides:
      • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
      • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
      • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
      • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
      • How to protect Linux against rogue USB devices using USBGuard

    Join Patreon

    Absolute vs Relative pathname

    The cd command changes the current directory when a directory name provided by the user. The name can be written as an absolute pathname (e.g. cd /etc/httpd/ ) or as local pathname relative to the root directory (e.g. cd conf.d/ ). For example:
    cd /etc/httpd/
    pwd
    ls
    cd conf.d/
    pwd
    ls

    The absolute vs. relative pathname for cd command in Linux

    Understanding . and .. directories

    On Linux the current directory is represented by a single dot ( . ) and two consecutive dots represent its parent directory ( .. ). Thus, to change to the parent of the current directory, run cd . .. For example:
    ls
    pwd
    cd ..
    pwd
    ls

    How can I return directly to my home directory when using the Linux terminal?

    How do I change directories in the Linux terminal and return to the previous directory?

    Simply pass the — option to the cd:
    cd —
    Verify it:
    pwd

    The -P option instructs cd to use the physical directory structure instead of following symbolic links:
    cd -P LinkDir
    pwd
    The -L option forces cd to follow symbolic links:
    cd -L LinkDir
    pwd

    Linux cd command cheat sheet

    Table 1: cd command under Linux
    Command Description
    cd Returns you to your login directory
    cd

    Also returns you to your login directory cd — Returns you to your previous working directory cd / Takes you to the entire system’s root directory. cd /root Takes you to the home directory of the root user. You must be the root user to access this directory. cd /home Takes you to the home directory, where user login directories are usually stored cd .. Takes you to the directory one level up. cd

    tom Takes you to tom’s home directory, if tom user has granted you permission cd /path/to/dir/ Take you to the /path/to/dir/ directory cd dir* Use a wildcard to change the directory name

    Conclusion

    The cd command is used to change the current directory in both Linux and other Unix-like systems. See Cd command wiki page.

    🐧 Get the latest tutorials on Linux, Open Source & DevOps via

    Источник

    How can I return to the previous working directory quickly in Bash?

    When I change into a directory with the cd command, I lose the previous working directory, unless I remember it in my memory. Is there some handy method to go back quickly?

    5 Answers 5

    You can go back to the last dir with cd —

    You can also do this

    As mentioned you can use cd — . The shell internally does a cd $OLDPWD .

    For usage in a script, you could use the OLDPWD shell variable: it contains the previous working directory.

    I prefer this over cd — in scripts because I don’t have to suppress any output.

    If you want to use it in a script and suppress the output, do this:

    Not the answer you’re looking for? Browse other questions tagged bash shell or ask your own question.

    Hot Network Questions

    Subscribe to RSS

    To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

    site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

    By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

    Источник

    How can I get back to last used directory in a Linux shell?

    How can I get back to the last directory used on a Linux shell?

    For example: I open a new shell (or a log in a different console shell) and I write :

    This will direct me to the /root/Desktop directory (if I have access to it)

    How can I now get back to the previous directory?
    Is it there any command to get back to the last directory I used?
    Did I need to again use the cd command to get back to previous directory?

    3 Answers 3

    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 ).
    Under dash 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.

    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.

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

    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.

    Источник

    How to go to the previous working directory in terminal?

    In terminal, how can I define a key to go to the previous directory which I was in when changing directory with the cd command?

    For example, I’m in /opt/soft/bin and I cd into /etc/squid3 and I want to get back to the first directory.

    6 Answers 6

    or you could use

    The other answers are definitely complete in the direct answer sense. cd — and cd $OLDPWD are definitely the main choices for this. However, I often find that getting into a workflow with pushd and popd works better.

    Long story short, if you are moving into a directory with the ultimate intent of coming back to where you started, use pushd / popd .

    Extended example

    The major difference is easily shown by an example.

    At this point, you have a directory stack that is dir2, dir1 . Running pushd with no arguments will put you back in dir1 with the stack now as dir1, dir2 . popd would do the same, but would leave you with an empty directory stack. This is not much different than how you would have been with the cd — workflow.

    However, now you can now change directories multiple times and get back to dir1 . For example,

    If you run popd at this point, you will go back to dir1 .

    Источник

    Go back to previous directory in shell

    Is there a way to go back to previous directory we were in using bash,tcsh without using pushd/popd ? I’d like to type something like «back» and got returned to the previous directory I was in.

    «cd -» works, but only for current and previous directories. Is there anyway I can go back to the previous previous previous directory like how you can go back in the web browser?

    4 Answers 4

    cd — (goes back to previous directory)

    If you want to be able to go to the other previous directories, this is not possible out of the box. But check this script and instructions:

    The cd command works as usual. The new feature is the history of the last 10 directories and the cd command expanded to display and access it. cd — (or simply pressing ctrl+w) shows the history. In front of every directory name you see a number. cd -num with the number you want jumps to the corresponding directory from the history.

    You can also use variable cd $OLDPWD . This can be also used in shell scripts.

    I find the easiest way to do it is with this .bashrc power edit: https://github.com/wting/autojump . You get to «mark» folders you navigate to, giving them a shorthand name that’s easy to remember (my advice; the foregoing is not in the docs), such as Pics for Pictures, etc. ‘jump’ returns you to the folder you ‘marked,’ and ‘marks’ lists folders you have added to the ‘stack’ (as with pushd and popd), with the added advantage that your marks remain the same from one session to the next, ad infinitum.

    I have yet to try it on more than one harddrive, but the results should be similar to those using a single volume.

    I think cd .. might help. If you do a ls -a in any directory you would see that there are two entries: one named «.» and another named «..»; the single dot is reference to the directory you are already in, while the double is the previous directory in the path.

    Источник

    Читайте также:  Linux монтирование дисков что это
    Оцените статью