Example to explain the use of CD command switching directory in Linux

Source: Internet
Author: User
Tags parent directory

The CD commands are familiar, and the bash shell is often used to switch directory commands. Next we get to know the CD command directly through the almighty man.

The Direct man CD is not going to work, and will show no manual entry for CD. The CD command is one of the basic commands for bash, so it should be man bash.

Related content, such as Man and Man page, is described in detail in a later Linuxshell command: Man.
The command syntax for seeing a CD by looking in the man page is as follows

The code is as follows:

CD [-l|[ -P [E]] [-@]] [dir]

Basic role

The function of the CD command is to jump from the current directory to Dir, and if the dir is left blank, it jumps to the value set in the environment variable home.

Additional content

environment variable Cdpath
For environment variable Cdpath,
If Cdpath is not set, the jump directory for each CD lookup is the parent directory with the current directory.
If there is a set Cdpath, the jump directory for each CD lookup is a set directory for the parent directory to find.
e.g. suppose you currently have a directory structure: ~/a/b/c if not set Cdpath

The code is as follows:

luckyshq@localhost:~$ CD A
luckyshq@localhost:~/a$ CD C
BASH:CD:C: No such file or directory

If the Cdpath is set to cdpath=.:/ Home/luckyshq/a/b.
The code is as follows:

luckyshq@localhost:~$ CD A
luckyshq@localhost:~/a$ CD C
luckyshq@localhost:~/a/b/c$

Setting the common directory to Cdpath can effectively improve the efficiency of the CD.
Cdpath in the directory: separated.
If you do not add the. Path (that is, the current path), this previously default directory as the parent directory CD is not available.
If the dir is at/at the beginning (that is, the absolute path), the Cdpath will fail.

Cd-p,-e parameter
Enter the actual directory in the shortcut directory. e.g. suppose ~ There are a,b two directories under a directory, and a sub directory B connected to B (this b is a shortcut)

The code is as follows:

No-P parameter
luckyshq@localhost:~$ CD A
luckyshq@localhost:~/a$ CD b

luckyshq@localhost:~/a/b$

Plus-P argument
luckyshq@localhost:~$ CD A
luckyshq@localhost:~/a$ Cd-p b
luckyshq@localhost:~/b$

If you put a folder of your own shortcuts in the folder, so that the script can be an infinite loop, the current path name will become infinitely long, but with the-p command can avoid the wireless loop.
If you add-e after-p, if the path does not have a corresponding physical path before the jump (for example, a path with multiple shortcuts as the name) will return a failed state. (e-specific usage may be realized in shell programming.)

Cd-l parameters
The-l parameter, contrary to-p (which cannot appear at the same time, i.e.-l|-p), jumps to the linked directory, similar to a CD with no parameters. (plus no----specifically what the difference is not clear =,=)

..
If the dir appears ...,
If.. After the path is followed, the first directory element in the path, such as a CD, is swallowed. /A/B/C equivalent CD b/c).
If.. After no path, it is returned to the parent directory of the current path.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.