What command is used to move to the previous working directory?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    cd command in linux known as change directory command. It is used to change current working directory. 

    Syntax:  

    $ cd [directory]

    To move inside a subdirectory : to move inside a subdirectory in linux we use 

    $ cd [directory_name]

    What command is used to move to the previous working directory?

    In the above example, we have checked number of directories in our home directory and moved inside the Documents directory by using cd Documents command. 

    Different functionalities of cd command : 

    • cd /: this command is used to change directory to the root directory, The root directory is the first directory in your filesystem hierarchy. 
       
    $ cd /

    What command is used to move to the previous working directory?

    • Above, / represents the root directory.
    • cd dir_1/dir_2/dir_3: This command is used to move inside a directory from a directory 
       
    $ cd dir_1/dir_2/dir_3

    What command is used to move to the previous working directory?

    • In above example, we have the document directory and inside the document directory we have a directory named geeksforgeeks and inside that directory we have example directory. To navigate example directory we have used command cd Documents/geeksforgeeks/example.
    • cd ~ : this command is used to change directory to the home directory. 
       
    $ cd ~

    What command is used to move to the previous working directory?

                  or 

    $ cd
    • cd : this command also work same as cd ~ command. 

    What command is used to move to the previous working directory?

    • cd .. : this command is used to move to the parent directory of current directory, or the directory one level up from the current directory. “..” represents parent directory. 
       
    $ cd ..

    What command is used to move to the previous working directory?

    • cd “dir name”: This command is used to navigate to a directory with white spaces.Instead of using double quotes we can use single quotes then also this command will work. 
    $ cd "dir name"

    What command is used to move to the previous working directory?

    • In above example, we have navigated the My songs directory by using cd “My songs” command. 
      or 
    $ cd dir\ name :
    • this command work same as cd “dir name” command. 

    What command is used to move to the previous working directory?

    This tutorial shows how to go back to the previous or parent directories using the cd (Change Directory) command. Additionally, this content includes a complete guide to move quickly between directories in the Linux terminal.

    This tutorial is optimized for inexperienced Linux users. By reading this documentation, they will fully understand how the cd (Change Directory) command works and its applications. All instructions described include screenshots, making it easy for every Linux user to understand and apply them.

    The cd command implementations to move between directories in the Linux terminal explained in this article include the following cd usage examples:

    The cd Command Options Function
    cd – Move to the previous directory
    cd .. Move to the parent directory
    cd ../.. Move to the parent directory of the parent directory (Two levels up)
    cd ../../.. Move three levels up
    cd <TAB-KEY> Autocomplete path or show available subdirectories
    cd Move to the home directory
    cd ~ Move to the home directory
    cd ~<User> Move to <User> home directory
    cd ‘Directory named with spaces’ Use quotation marks to move to a directory with spaces in name

    The Linux Directory Structure (For New Linux Users)

    Note: If you already know the basic Linux directory structure, you can jump straight to instructions to go back to previous or parent directories.

    First, I want to remind new Linux users that the Linux directory structure is hierarchical. Directories within directories are subdirectories. The directories and subdirectories tree are what we call the “path”. When we specify a path, we are specifying parent directories and subdirectories.

    In Linux, the main directory is what we call the root directory. It is the top directory containing the rest of the system directories. Users see a slash after their hostname when the current directory is the root directory, as shown in the following image (Violet slash).

    What command is used to move to the previous working directory?

    The root directory contains system core directories, including the /boot directory, which contains boot partition and/or directories and files, and the /usr partition and/or directory, which contains program directories and files.

    The following image shows the / (root) directory content, most of which is universal for all Linux distributions:

    What command is used to move to the previous working directory?

    For example, in the path /etc/apt/, the root directory / (First slash) is the parent directory of the /etc subdirectory, which is the parent directory of the /apt directory.

    This section explains how to go back to your previous current directory and to go back to a parent directory using the cd command in Linux.

    In the following example, the linuxhint user’s first current directory is his home directory. In the first line, the user uses the cd command to move to the /etc directory, following the hierarchical path beginning from the root directory.

    To go back from the current directory (/etc/) to the previous one, the user runs the cd command followed by a hyphen, as shown in the second line of the following figure:

    What command is used to move to the previous working directory?

    As you can see in the previous image, the user returned to the previous directory by running cd -.

    The previous example explains how to return to the previous directory the user was in.

    Now, let’s say the user does not want to return to the previous directory he was in, but he wants to go back to the hierarchical tree.

    Here, a user is in his home directory, and he moves to the /etc/apt subdirectory. Instead of returning to the previous directory he was in (His home directory), he wants to move to the parent directory of the current one (In this case, the parent directory of /apt is /etc). For this, the user will run the command cd followed by two dots, as shown below:

    What command is used to move to the previous working directory?

    As you can see in the previous figure, the user moved to the parent directory of the previous one (/etc) and not to the previous directory he was in (/home/linuxhint).

    As you can see, cd – moves the user to his previous current directory, while cd .. moves him to the current parent directory.

    How To Move to the Parent Directory of the Current Parent Directory (Two Levels Up)

    As said in the previous example, the cd .. command allows us to move to the parent directory. If the user wants to move to the parent directory of the parent directory (Two levels up), he needs to type the two dots twice, separated by a slash.

    In the following example, the user’s current directory is linuxhint2, located under the linuxhint directory, which is located under the Desktop directory within the user home.

    Let’s say the user wants to move two levels up, from the linuxhint2 directory to the Desktop directory. In this case, he only needs to use the two dots twice, separated by a slash, as shown in the following screenshot:

    What command is used to move to the previous working directory?

    The previously executed command instructs Bash to take the user two directories up in the hierarchical tree. You also would implement additional dots and slashes to move to upper levels in the directory tree.

    Using the TAB Key With the cd Command to Autocomplete Paths

    The keyboard key tab plays an important role when using the cd command. It helps users move between directories without typing the whole path and without knowing the final path in advance.

    In the following example, four subdirectories begin with the same name, but only the last parts of their names change: such as linuxhint, linuxhint2, and linuxhint3.

    Suppose the user runs cd linuxhint and presses the TAB key twice. Then, the console returns all existing subdirectories, whose names begin with the path typed by the user. Thus, informing the user of the available subdirectories for him to type the complete full path.

    In the following image, the user executed cd Desktop/linuxhint and then pressed the TAB key:

    What command is used to move to the previous working directory?

    Even if the user does partially type the destination full path, the TAB key will also show all available subdirectories within the directory we want to access.

    In the following example, the user knows he wants to access a subdirectory within his home directory. But he does not know what is the specific subdirectory he wants to access.

    In this case, the user can run cd /parentdirectory/ and press the TAB key twice. This will print all subdirectories within the parent directory:

    What command is used to move to the previous working directory?

    How To Move to the Home Directory in the Linux Terminal

    Moving to your home directory is pretty simple. Just run the cd command without additional options, as shown below:

    What command is used to move to the previous working directory?

    You also can use a tilde (~) to specify your home directory as a path. By running the cd command followed by a tilde, you will move to your home directory.

    What command is used to move to the previous working directory?

    You can use the tilde to move to any user’s home directory. To accomplish it, type the username whose directory you want to move to, after the tilde symbol. As shown in the following example, the linuxhint user moves to the torvalds directory.

    What command is used to move to the previous working directory?

    Move to a Directory With Spaces in its Name

    Let’s assume the user wants to move to a directory whose name contains spaces to end this tutorial. In this case, the user only needs to type the directory name between quotation marks, as shown in the following screenshot.

    In the following example, the user implements quotation marks to move into the Linux Hint directory:

    What command is used to move to the previous working directory?

    Conclusion

    The article discussed how the cd command allows users to move between different locations faster than the graphical user interface. All commands shown in this content are easy to learn and implement, and required for any Linux user.

    The cd command is also available in other operating systems, like macOS or MSDOS, and the command is universal for all Linux distributions. I recommend readers to utilize the provided examples to make terminal browsing easier.

    I hope all tips provided are useful for new users. Keep following us for more professional Linux articles.

    About the author

    What command is used to move to the previous working directory?

    David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

    How do I go back to previous directory?

    This tutorial shows how to go back to the previous or parent directories using the cd (Change Directory) command. ... How To Go Back to a Directory in Linux..

    What is the command to return to a previous level of directory?

    for each level you want to go up. When you want to go back, type cd - and you will be back where you started.

    How do I find my previous working directory in Linux?

    The cd (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems. It is one of the most basic and frequently used commands when working on the Linux terminal.

    Which shortcut represents the previous working directory?

    A Few Shortcuts Typing cd - changes the working directory to the previous one.