To change directories from your current working directory, use the command cd. Show
The above command changes directories to the /tmp/ directory. The second word on the command line must be a path. It can either be relative or absolute, and can move one directory or many. If the cd command is entered at the shell prompt with no options or path specified, the default action is to move the user into their home directory. For example, user someone would be moved to the /home/someone/ directory. The cd has many useful options. You can see all of these options by entering man cd at the shell prompt. The most commonly used options are listed below.
Below are a few examples of the use of cd.
If you attempt to cd into a directory you do not have permission to access, you are denied permission to access that directory. Denying access to the root and other users' accounts (or home directories) is one way your Red Hat Enterprise Linux system prevents accidental or malicious tampering. Refer to Section 4.11 Ownership and Permissions for more information. Introduction Many Linux commands, such as the ls command, affect the current working directory. The current working directory is the directory your terminal window or command prompt is working in. Linux treats the Home directory as the default working directory. Using the cd command in Linux allows you to change the current working directory. In this tutorial, we will explain the cd command syntax and show examples of how you can use it. Prerequisites
Linux CD Command SyntaxThe cd command in Linux uses the following syntax: cd [options] [directory]In the command above:
For instance, to move to the Desktop directory, run: cd /home/phoenixnap/DesktopIf the command executes successfully, the current working directory is indicated in the terminal interface: If the terminal interface does not indicate the current working directory, using the pwd command displays it as the output: pwdThe cd command uses the following options:
The Linux cd command offers several ways to navigate and change the working directory using the terminal window. It lets you change directories using relative and absolute paths, move to parent or root directories, or find directories with incomplete names. Note: The cd command is a built-in shell command. This means that its behavior varies slightly between shells since it uses shell environment variables. Learn more in our guide to environment variables in Linux. Changing DirectoryTo change to a new working directory, use the cd command with a directory path. cd [path to directory]For instance, moving to Example_Directory, located in the Home directory: cd Example_DirectoryChange Directory and List ContentAppend the ls command to the cd command using the && flag to change to a new directory and list its content simultaneously. cd [path to directory] && lsUsing the previous example: cd Example_Directory && lsChanging Directory Using an Absolute PathUsing an absolute path to the directory means that the path starts from the root directory. For instance, changing to the Downloads directory using its absolute path: cd /home/phoenixnap/DownloadsChanging Directory Using a Relative PathA relative path is a path to a directory relative to the current working directory. A relative path is best used when changing to a subdirectory of the current working directory. In the example above, the Downloads directory is a subdirectory of Home. In this case, a relative path is a result of omitting the path to the current directory from the path to the new working directory: cd DownloadsSince the path to the current directory is /home/phoenixnap, omitting that part of the absolute path to the Downloads directory (/home/phoenixnap/Downloads) results in a relative path (Downloads). Changing to the Previous DirectoryAdding a dash symbol (-) to the cd command returns the shell to the previous working directory. For instance, after moving from Downloads to Example_Directory, return to Downloads with: cd -Changing to Parent DirectoryTo change to the parent of the current directory, add two period symbols (..) to the cd command. For example, to move from Example01 to its parent directory Example_Directory: cd ..Changing to the Root DirectoryAdd the slash symbol (/) to the cd command to move into the system's working directory: cd /Changing Back to the Home DirectoryIn Linux, the Home directory represents the default working directory. Using the cd command without any options or path changes back to the default working directory: cdThe absence of the current working directory path indicates that you are in the default working directory: Another way to do this is to add the tilde symbol (~) to the cd command: cd ~Changing to Another User's Home DirectoryChange to another user's Home directory by adding the tilde symbol (~) appended with the appropriate username. cd ~[username]For instance, changing to the Home directory of a user named alex: cd ~alexChanging to a Directory with Spaces in the NameIf the directory name contains blank spaces, change to it by surrounding the name with single quotation marks (' '). Alternatively, append a backslash symbol (\) to every word in the name except the last one: cd 'Directory name with blank spaces' cd Directory\ name\ with\ blank\ spacesFor example, changing to a directory named This is a directory: cd 'This is a directory' cd This\ is\ a\ directoryAutocomplete Directory NameIf you don't know the name of the directory you are trying to move to, the Linux terminal offers an autocomplete feature. After you start typing the directory name, press the Tab button on your keyboard to get autocomplete suggestions. For instance, if you know that the name of the directory starts with an X (for instance, XYZ), type: cd X[Tab]Conclusion After reading this tutorial, you should be able to use the Linux cd command to navigate and change the current working directory in the terminal window. Learn more about other Linux commands in our Linux commands cheat sheet. How do you navigate from one directory to another?We'll use "cd" to move down as well as up the directory structure. The second way to list files in a directory, is to first move into the directory using the "cd" command (which stands for "change directory", then simply use the "ls" command.
What command is used to navigate directories?The cd command allows you to move between directories. The cd command takes an argument, usually the name of the folder you want to move to, so the full command is cd your-directory . Now that we moved to your Desktop, you can type ls again, then cd into it.
Which shortcut represents the previous working directory?A Few Shortcuts
Typing cd - changes the working directory to the previous one.
How do I go back to a previous folder in command prompt?When you want to go back, type cd - and you will be back where you started.
|