Linux cd Command Example

When working on the Linux command line, cd command is the most basic and most commonly used commands. cd command stands for "change directory" to change the current working directory for Linux and other Unix-like operating system. The current working directory is the user's current working directory. Every time when interacting with the command prompt, you are working in the directory.

In this tutorial, we'll show you how to use the cd command to navigate the directory tree of your computer.

cd command

cd is a built-in shell, its behavior may be due to the shell to another. It uses the shell environment variable to determine the information necessary for its execution. We will introduce Bash built-in version of the cd.

Cd command syntax is as follows:

cd [OPTIONS] directory

This command only accepts two option rarely used.

  • -L, follow symbolic links. By default, cd acts like -L option is specified the same.
  • -P, do not follow symbolic links. In other words, when you specify this option and attempt to navigate to a directory symbolic link, cd will change the actual directory.

In the absence of any arguments case, cd will take you to your home directory.

When browsing the file system, you can use the Tab key to automatically complete the directory name. Adding a slash at the end of the directory name is optional. In order to be able to switch to the directory, the user must have permission to perform the directory.

To find out your current directory, use the pwd command.

Absolute path name and relative path name

When you specify a directory, you can use an absolute path name or relative path name. Absolute or full path of the system root / start, the relative path from the current directory.

By default, when you log on Linux systems, your current working directory is set to your home directory. Downloads assumed that there is a directory in your home directory, you can use a relative path to navigate a directory to the directory:

cd Downloads

You can also use the absolute path to navigate to the same directory:

cd /home/username/Downloads

In short, if the path begins with a slash (/), it is the absolute path to the directory.

Parent directory

On the Unix-like operating systems, the current working directory (.) Is represented by a single point. The next two points (..) represents the parent directory, in other words, above the current directory.

If you type, cd. You will change the current directory, or simply, the command will do nothing.

Let's say you currently in / usr / local / share directory, to switch to / usr / local directory (one up from the current directory), you can type:

cd ../

To move two levels to the / usr directory (the parent's parent), you can run the following command:

cd ../../

In another example to. Suppose you in / usr / local / share directory and you want to switch to the / usr / local / src. You can enter the following command:

cd ../src

Navigate to Previous Directory

To change back to the previous working directory, set the dash (-) character as an argument for the cd command:

cd -

Navigate to the home directory

To navigate to your home directory, simply enter cd. Another method is to directly return to the main directory using the tilde (~) character, as follows:

cd ~

For example, if you want to navigate to the Downloads directory in your home directory, you would type:

cd ~/Downloads

You can also use the following syntax to navigate to the home directory of other users:

cd ~username

The name of the directory contains spaces

If you want to change the directory name contains spaces, use quotation marks around the path or use the backslash (\) character to escape the space:

cd 'Dir name with space'
cd Dir\ name\ with\ space

in conclusion

By now, you should have a good understanding of what the current working directory is and how to use the cd command to navigate to a different directory.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159882.htm