linux命令中cd使用Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock Some Linux commands are so familiar, we don’t even notice we’re using them. Thecd command for changing ...
Some Linux commands are so familiar, we don’t even notice we’re using them. The cd command for changing directories is one of these. There are some tricks that can help you become more efficient with cd—or you can ditch it, altogether.
You blink all day, every day, but, most of the time, you’re unaware of it. Unless something gets in your eye, you rarely think about that little, regular movement. Some Linux commands are like that. They hover on the periphery of your consciousness. Even though you use them daily, they don’t catch your attention because they’re so small and simple.
Within the first hour of using a Linux computer, you learn how to use the cd command included with Bash and other shells. Perhaps you had prior experience using it on another operating system and didn’t need an explanation. It changes the current working directory, right? What else is there to know?
You don’t have to type the whole directory path; you can use auto-complete. For each part of a path, after you type enough letters to distinguish the name of the directory from the others, press Tab to auto-complete the directory name.
The shell adds a trailing forward slash so you can repeat the tab-completion process. That’s also why there’s a trailing forward slash on the first command. There isn’t one on the second because that one was typed.
These are examples of absolute paths, in which you provide the entire path from the root of the filesystem to the target directory, to cd.
Relative paths are referenced from the current working directory. In the home directory, there’s a directory called work . You can use the tree command to see the directory tree inside the work directory—just type the following:
The work directory contains a directory called dev . There’s also a directory called dev in the root directory of the filesystem. You can use ls with -d (directory) to look at each of these. The -hl (human-readable, long listing) option tells ls to use easy to read units for the directory sizes, and the long format listing.
If you type dev, the shell assumes you mean the “dev” in the current directory. To force it to look at the “dev” in the root directory, just add a leading forward slash to represent the root of the filesystem, as shown below:
Without a leading forward slash, longer paths are assumed to start from the current working directory, too, as shown below:
更改目录双点 (Changing the Directory with Double Dot)
The double dot identifier represents the parent directory of the current working one. If you’re in a deeply nested subdirectory, you can use .. with cd to move to the parent directory of the one you’re in.
The name of the directory you’re moving to appears before you move into it.
另一种相对 (Another Kind of Relative)
The shell uses the current working directory as the “root” or base directory for relative paths. You can use the CDPATH environment variable to set another location as the base directory for relative paths. If you spend most of your time in a certain section of the filesystem tree, this can save you a lot of keystrokes (and time) every day.
Let’s type the following to make work/dev/projects the base directory for relative paths:
Now, each time you use the dc command, the location in the CDPATH environment variable is checked first for matching directory names. If any of them match the target you provided in the cd command, you’re transferred to that directory.
Now, regardless of where you are in the filesystem, when you use the cd command, the shell checks whether the target directory is located in the base directory. If it is, you’re moved to that target directory.
If your target directory starts with a leading forward slash (/), which makes it an absolute path, it won’t be affected by the CDPATH environment variable.
如果目标目录以正斜杠( / )开头(使其成为绝对路径)，则该目录将不受CDPATH环境变量的影响。
To demonstrate this, we type the following:
The CDPATH environment variable is truly a path, just like the PATH environment variable. When you type a command, the shell searches the locations in the PATH for a match. When you use CDPATH, the shell searches the locations in the CDPATH environment variable for a match. Also, the same as PATH, CDPATH can contain multiple locations.
To make your settings permanent, you have to add them to a configuration file, such as .bashrc.
One thing to be aware of: If you set a base directory, it also affects directory changes performed within scripts. To avoid this, you can use absolute paths in your scripts or a test in your .bashrc file when you specify your CDPATH, as shown below:
The cdspell option checks your directory names and corrects some common typing mistakes, including transposed or missing characters, or names with too many characters. If it finds a directory that matches any of the corrections, the corrected path is printed, and the cd action takes place.
The shell caught the error, corrected it, and changed to the “Desktop” directory.
Another shopt option you can use with cd is autocd. It eliminates the need for you to type cd at all. Anything you type that isn’t a command, script, or other executable (such as an alias), is used as a target directory. If you can transfer to that directory, it’s printed in the terminal window, and you’re changed to that directory.