cd (更改shell的当前工作目录)

rose1 发表于 2020-08-27 12:00
浏览次数:
在手机上阅读

在类似Unix的操作系统上,cd命令(“更改目录”)更改shell的当前工作目录。 本文档介绍了cd的bash 内置版本。

查看英文版

目录

1 cd 运行系统环境

2 cd 说明

3 cd 语法

4 cd 例子

cd 运行系统环境

Linux

cd 说明

cd是您在命令行上最常使用的命令之一。它将更改您的工作目录。使用它可以在文件系统的层次结构中移动。
cd is among the commands you will use most often on the command line. It changes your working directory. Use it to move around in the hierarchy of your file system.

查看英文版

查看中文版

cd 语法

cd [-L | -P [-e]] directory

选件

-L

强制遵循符号链接。换句话说,如果您告诉cd移至“目录”,它实际上是指向目录的符号链接,则它将移至该符号链接指向的目录中。

此选项是cd的默认行为;通常,它将始终像指定-L一样工作。

-P

使用物理目录结构而不使用符号链接。换句话说,只有在实际存在的情况下,才更改为指定的目录。不会跟随符号链接。此选项与-L选项相反,如果同时指定了两个选项,则将忽略此选项。

-e

如果指定了-P选项,并且无法确定当前的工作目录,则此选项告诉cd退出并出现错误。如果未与该选项一起指定-P,则此选项不起作用。

目录

为了帮助您组织文件,文件系统包含称为目录的特殊文件。可以将它们视为文件柜中的文件夹:它们具有名称,如文件,但其功能是“包含”其他文件和其他目录。这样,您可以将系统上的文件分开并根据文件的功能或目的进行分类。

系统上的所有文件和目录都来自一个主目录:根目录。根目录“上方”没有目录;所有其他目录都在根目录“下方”。

包含在另一个目录中的任何目录都称为子目录。子目录从分支“目录”的“根”“分支”。与真正的树不同,目录树是上下颠倒的:根在顶部,而分支则向下。当您进入子目录时,您是在树的“下”移动。当您移动到目录的父目录中时,您是在树上“向上”移动。

文件系统上的所有目录都是根目录的子目录。

注意

默认情况下,当您打开终端并开始使用命令行时,您将位于主目录中。

目录的表示方式

目录之间用正斜杠(“ / ”)分隔。例如,目录名称“ documents / work / accounting ”的意思是“目录名称为accounting,该目录位于名称为work的目录中,该目录位于当前目录中的documents 目录中。”

要切换到该目录并使其成为我们的工作目录,可以使用以下命令:

cd documents/work/accounting

如果目录名称的第一个字符是斜杠,则表示目录路径始于根目录。因此,与上面的示例相反,目录名称“ / documents / work / accounting ”(请注意以斜杠开头)表示“目录名称为accounting,该目录位于名称为work的目录中,而该目录位于名为document的目录中,在根目录中。”

要切换到该目录并使其成为我们的工作目录,可以使用以下命令:

cd /documents/work/accounting

根目录

根目录是文件系统层次结构中的第一个目录。所有其他目录是根目录的子目录。

根目录由一个斜杠(“ / ”)表示。

要切换到根目录,使其成为您的工作目录,请使用以下命令:

cd /
警告

除非您以root或使用sudo命令登录,否则您将无法对系统上的根目录进行任何更改。除非您确定自己在做什么,否则请不要在此处进行任何更改。犯错误可能会破坏您的系统!

工作目录

当前目录(无论是哪个目录)都由一个点(“ 。”)表示。

因此,运行以下命令:

cd .

...会将我们更改为当前目录。换句话说,它什么也没做。

实际发生的是点代表“假定”目录;它是一个占位符,您可以在目录名称中的任何位置使用点。因此,命令:

cd documents

...与命令相同:

cd ./documents

...也与:

cd documents/.

...以及:

cd ./documents/.

在所有这些示例中,点代表“假定存在的目录”。您可以在要告诉外壳程序目录进入该位置并采用适当值的任何位置将其用作占位符。

父目录

当前目录的父目录(换句话说,是当前目录的上一级目录,其中包含我们现在所在的目录)由两个点(“ .. ”)表示。

因此,如果我们在目录/ home / username / documents中,则执行命令:

cd ..

...我们将放置在目录/ home / username中。

可以在目录名称中的任何位置使用双点(“ ..”)目录符号来表示上一级。例如,如果我们有两个目录/ home / username / documents/ home / username / downloads,而我们当前在/ home / username / documents中,则可以键入以下内容:

cd ../downloads

...我们将被放置在/ home / username / downloads中。

这是另一个“无用”命令,但它说明了您可以使用双点符号的方式:

cd documents/..

...这会将我们放置在子目录文档上方一层的目录中,也就是当前目录。请注意,这仅在子目录文档已存在的情况下有效。

同样,命令:

cd documents/../documents

...在功能上与以下命令相同:

cd documents

您的主目录

默认情况下,打开新的终端会话时,主目录就是您所放置的目录。该目录包含所有设置,邮件,默认文档和下载文件夹以及其他个人物品。它有一个特殊的表示形式:波浪号(“ 〜 ”)。

因此,如果我们的用户名是username,而我们的主目录是/ home / username,则命令:

cd ~

...在功能上与命令相同:

cd /home/username

...并且我们始终可以通过将代字号作为目录名称的第一部分来访问主目录的子目录。例如,如果您的documents文件夹名为/ home / username / documents,则始终可以使用以下命令移至该目录:

cd ~/documents

上一个工作目录

更改目录后,可以通过用短划线(“ - ”)表示它来回到上一个工作目录。执行此操作时,shell会自动告诉您新的目录名称。

因此,例如:

cd ~
pwd
/home/hope
cd Documents/financial
pwd
/home/hope/Documents/financial
cd -
/home/hope
pwd
/home/hope

使用斜杠

在目录名末尾使用斜杠是可选的。目录被视为文件,因此您无需将目录放在文件中。但是,如果确实将其放在此处,则系统肯定会确定您期望该文件为目录。例如,如果当前目录中有一个名为dirname的子目录,则命令:

cd dirname

...与命令相同:

cd dirname/

该命令的第二种形式明确指出dirname是目录,但是两个命令都是等效的。

cd [-L | -P [-e]] directory

Options

-L

Force symbolic links to be followed. In other words, if you tell cd to move into a "directory", which is actually a symbolic link to a directory, it moves into the directory the symbolic link points to.

This option is the default behavior of cd; normally, it will always act as if -L was specified.

-P

Use the physical directory structure without following symbolic links. In other words, only change into the specified directory if it actually exists as named; symbolic links will not be followed. This option is the opposite of the -L option, and if they are both specified, this option will be ignored.

-e

If the -P option is specified, and the current working directory cannot be determined, this option tells cd to exit with an error. If -P is not specified along with this option, this option has no function.

Directories

To help you organize your files, your file system contains special files called directories. Think of them like folders in a file cabinet: they have names, like files, but their function is to "contain" other files, and other directories. In this way, you can keep the files on your system separate and sorted according to their function or purpose.

All files and directories on your system stem from one main directory: the root directory. There are no directories "above" the root directory; all other directories are "below" the root directory.

Any directory contained inside another directory is called a subdirectory. Subdirectories "branch" off the "root" of the directory "tree." Unlike a real tree, directory trees are upside-down: the root is at the top and the branches reach down. When you move into a subdirectory, you are moving "down" the tree; when you move into a directory's parent directory, you are moving "up" the tree.

All directories on your file system are subdirectories of the root directory.

Note

By default, when you open a terminal and begin using the command line, you are placed in your home directory.

How directories Are represented

Directories are separated by a forward slash ("/"). For instance, the directory name "documents/work/accounting" means "the directory named accounting, which is in the directory named work, which is in the directory named documents, which is in the current directory."

To change into this directory, and make it our working directory, we would use the command:

cd documents/work/accounting

If the first character of a directory name is a slash, that denotes that the directory path begins in the root directory. So, in contrast to the example above, the directory name "/documents/work/accounting" (note the beginning slash) means "the directory named accounting, which is in the directory named work, which is in the directory named documents, which is in the root directory."

To change into this directory, making it our working directory, we would use the command:

cd /documents/work/accounting

The root directory

The root directory is the first directory in your filesystem hierarchy. All other directories are subdirectories of the root directory.

The root directory is represented by a single slash ("/").

To change into the root directory, making it your working directory, use the command:

cd /
Caution

You will not be able to make any changes to the root directory on your system unless you are logged in as root, or using the sudocommand. Unless you are certain of what you're doing, don't make any changes here. Making a mistake could destroy your system!

The working directory

The current directory, regardless of which directory it is, is represented by a single dot (".").

So, running this command:

cd .

...would change us into the current directory. In other words, it would do nothing.

What's actually happening is the dot represents the "assumed" directory; it's a placeholder, and you can use the dot anywhere in a directory name. So, the command:

cd documents

...is the same as the command:

cd ./documents

...and also the same as:

cd documents/.

...as well as:

cd ./documents/.

In all of these examples, the dot represents "the directory assumed to be there". You can use it as a placeholder anywhere you want to tell the shell that a directory goes in that place, and to assume the appropriate value.

The parent directory

The parent directory of the current directory — in other words, the directory one level up from the current directory, which contains the directory we're in now — is represented by two dots ("..").

So, If we were in the directory /home/username/documents, and we executed the command:

cd ..

...we would be placed in the directory /home/username.

The double-dot ("..") directory notation can be used anywhere in a directory name to represent going up one level. For instance, if we have two directories, /home/username/documents and /home/username/downloads, and we are currently in /home/username/documents, we could type the following:

cd ../downloads

...and we would be placed in /home/username/downloads.

Another "useless" command, but one that illustrates the way you can use the double-dot notation, is this one:

cd documents/..

...which will place us in the directory one level above the subdirectory documents — in other words, the current directory. Note that this will only work if the subdirectory documents already exists.

Similarly, the command:

cd documents/../documents

...is functionally the same as this command:

cd documents

Your home directory

Your home directory is the directory you're placed in, by default, when you open a new terminal session. It's the directory that holds all your settings, your mail, your default documents and downloads folder, and other personal items. It has a special representation: a tilde ("~").

So, if our username is username, and our home directory is /home/username, the command:

cd ~

...is functionally the same as the command:

cd /home/username

...and we can always access the subdirectories of our home directory by placing the tilde as the first component of the directory name. For instance, if your documents folder is named /home/username/documents, you can always move into that directory using the command:

cd ~/documents

The previous working directory

After you change directory, you can change back to the previous working directory by representing it with a dash ("-"). When you do this, the shell will automatically tell you the new directory name.

So, for instance:

cd ~
pwd
/home/hope
cd Documents/financial
pwd
/home/hope/Documents/financial
cd -
/home/hope
pwd
/home/hope

Using a trailing slash

Using a slash at the end of a directory name is optional. Directories are treated as files, so you don't need to put it there; but if you do put it there, the system knows for sure you're expecting that file to be a directory. For example, if there is a subdirectory in the current directory named dirname, the command:

cd dirname

...is the same as the command:

cd dirname/

The second form of the command explicitly states that dirname is a directory, but both commands are equivalent.

查看英文版

查看中文版

cd 例子

cd hope

上面的示例将工作目录更改为hope子目录(如果存在)。

cd ../computerhope

上面的示例将上一级遍历到父目录,然后下至目录computerhope。

cd ../../

在目录树中遍历两个目录。换句话说,移至包含目录的目录,该目录包含当前的工作目录。

cd hope

The above example would change the working directory to the hope subdirectory if it exists.

cd ../computerhope

The above example would traverse up one level to the parent directory and then down into the directory computerhope.

cd ../../

Traverse two directories up the directory tree. In other words, move into the directory which contains the directory which contains the current working directory.

查看英文版

查看中文版

其他命令行

cut | cu | csplit | crontab | cpio | continue | compress | col | cmp | cksum | chsh | chroot | chkey | chmod | cp | comm | chown | cal | calendar | clear | chfn | cancel | cat | cc | cfdisk | checkeq | checknr | chgrp |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条