如何在CMD(MS-DOS和PowerShell)中更改目录?

cmd or cmd.exe or MS-DOS is a command-line tool used in Windows operating systems. One of the most basic operations for the command line is changing directory. We can change the current working directory with the cd command. It may seem very basic and easy but a changing directory with the cd command provides a lot of different use cases like the change to a parent directory, change the drive, change to the root directory. In this tutorial, we will learn all of them.

cmdcmd.exeMS-DOS是Windows操作系统中使用的命令行工具。 命令行最基本的操作之一是更改目录。 我们可以使用cd命令更改当前工作目录。 看起来非常基本和容易,但是使用cd命令更改目录提供了许多不同的用例,例如更改为父目录,更改驱动器,更改为根目录。 在本教程中,我们将学习所有这些。

cd命令语法 (cd Command Syntax)

cd command has very simple syntax where we provide the path. There is also a drive part but it is used very rarely and optionally. We can also use  chdir command like cdcommand which is the short form of chdir.

cd命令具有非常简单的语法,我们在其中提供路径。 还有一个驱动器部件,但很少使用,也可以选择使用。 我们也可以使用chdir命令,例如cd命令,它是chdir的缩写。

cd DRIVE:PATH

chdir DRIVE:PATH
  • DRIVE is optional and specifies the drive or partition like C: , D: we can also use lowercase like c:, d:. DRIVE is generally not used.

    DRIVE是可选的,它指定驱动器或分区,例如C:,D:,我们也可以使用小写字母,例如c:d: :。 通常不使用DRIVE。

  • PATH is the most important part where the directory names, . and .. are used to specify the path.

    PATH是目录名称最重要的部分...用于指定路径。

cd命令帮​​助 (cd Command Help)

As cd is a very simple command it has also very little helpful information for complete usage. We can print help information with /? like below.

由于cd是一个非常简单的命令,因此对于完全使用它也几乎没有有用的信息。 我们可以使用/?打印帮助信息/? 像下面一样。

> cd /?
cd Command Help
cd Command Help
cd命令帮​​助

变更目录(Change Directory)

We will start with a simple usage where we will provide the directory we want to change. The given directory is a child directory of the current or current working directory. In this example, we will change to the directory named Users, ismail , Desktop in a row.

我们将从一个简单的用法开始,我们将提供我们想要更改的目录。 给定目录是当前或当前工作目录的子目录。 在此示例中,我们将连续更改到名为UsersismailDesktop的目录。

> cd Users

> cd ismail

> cd Desktop
Change Directory
Change Directory
变更目录

使用制表符完成目录名称(Using Tab To Complete Directory Name)

MS-DOS provides tab shortcuts to complete directory. If we do not know the whole directory name or it is too long to type we can use TAB key to list available directories. The completion will be done according to the provided directory name. For example, if we typed U and press TAB the directories like User, Users, Use will be listed. If we do not type any directory name all currently existing directories will be enumerated alphabetically.

MS-DOS提供了用于完成目录的选项卡快捷方式。 如果我们不知道整个目录名称,或者键入时间太长,可以使用TAB键列出可用目录。 将根据提供的目录名称完成此操作。 例如,如果我们键入U并按TAB键,则会列出诸如UserUsersUse的目录。 如果我们不输入任何目录名,则将按字母顺序枚举当前所有现有目录。

> cd <TAB>

更改多个级别/目录 (Change Multiple Level/Directories)

In the previous example, we have changed the directories one by one with multiple  cd commands. We can also specify multiple directories in a single cd command. In this example, we will change to the Users, ismail, Desktop.

在前面的示例中,我们使用多个cd命令一个一个地更改了目录。 我们还可以在一个cd命令中指定多个目录。 在此示例中,我们将更改为UsersismailDesktop

> cd Users\ismail\Desktop

使用空格更改为目录名称 (Change To Directory Name with Spaces)

Directory names can be upper case, lower case, and provide some special characters like space. While navigating between directories with spaces we have to take care of the directory name. In order to prevent errors, we can use a double quote for the directory name with spaces or for the whole path like below. For example, in order to change the directory to the Program Files we should use "Program Files" like below.

目录名称可以是大写,小写,并提供一些特殊字符,例如空格。 在带有空格的目录之间导航时,我们必须注意目录名称。 为了防止错误,我们可以对目录名称使用双引号,并使用空格或整个路径,如下所示。 例如,为了将目录更改为Program Files我们应使用如下所示的"Program Files"

> cd "Program Files"
Change To Directory Name with Spaces
Change To Directory Name with Spaces
使用空格更改为目录名称

更改为上层目录/父目录(Change To Upper/Parent Directory)

In order to change one level upper or parent directory typing, the whole path is not feasible. We can use .. double dot which simply references one level upper or parent directory.

为了更改上一级或上一级目录的键入,整个路径是不可行的。 我们可以使用..双点,它仅引用上一级目录或父目录。

> cd ..

> cd "../Program Files"
Change To Upper/Parent Directory
Change To Upper/Parent Directory
更改为上层目录/父目录

更改为二级上级/父级目录(Change To Two Level Upper/Parent Directory)

We can also use .. to change multiple levels of upper directories. In this example, we will go two-level upper directory with multiple  ... We will also use / in order to specify the directory level.

我们还可以使用..更改上层目录的多个级别。 在此示例中,我们将进入具有多个..的二级目录。 我们还将使用/来指定目录级别。

> cd ../..

更改为同级目录 (Change To Sibling Directory)

We can change to the sibling directory. We will use .. to go one level upper directory and provide the sibling directory name like Downloads.

我们可以转到同级目录。 我们将使用..进入上一级目录,并提供兄弟目录名称,如Downloads

> cd ../Downloads
Change To Sibling Directory
Change To Sibling Directory
更改为同级目录

更改为根目录(Change To Root Directory)

The root directory is the highest level of the directory for the current partition or drive. We can directly change to the root directory with the cd\ command without providing extra information.

根目录是当前分区或驱动器目录的最高级别。 我们可以使用cd\命令直接更改为根目录,而无需提供其他信息。

> cd\
Change To Root Directory
Change To Root Directory
更改为根目录

打印/显示当前工作目录(Print/Display Current Working Directory)

While working and changing directories we may be lost in the paths. In some cases, we may need to print the current directory. The current working directory is the path we are currently working and can be printed with the %cd% environment variable like below. We will also use echo command to print the current working directory environment variable to the cmd.

在工作和更改目录时,我们可能会迷失在路径中。 在某些情况下,我们可能需要打印当前目录。 当前工作目录是我们当前正在工作的路径,可以使用如下所示的%cd%环境变量进行打印。 我们还将使用echo命令将当前工作目录环境变量打印到cmd。

> echo %cd%
Print/Display Current Working Directory
Print/Display Current Working Directory
打印/显示当前工作目录

更改驱动器(Change Drive)

Windows operating system generally used with multiple partitions or drives. While working with the  cd command navigation between these drives may be required. We can change the current drive or partition just by providing the destination drive or partition name like d: , e: etc. In the following example, we will change the current working partition to the D.

Windows操作系统,通常与多个分区或驱动器一起使用。 在使用cd命令时,可能需要在这些驱动器之间导航。 我们可以通过提供目标驱动器或分区名称(例如d:e:等)来更改当前驱动器或分区。在以下示例中,我们将当前工作分区更改为D

> cd d:

更改驱动器和目录 (Change Drive and Directory)

We can also change the current working partition and directory in a single and same command. WEe just need to provide the partition/drive name with the path or directory we want to change. In this example, we will change to the D partition Backups\2019.

我们还可以通过一个相同的命令更改当前的工作分区和目录。 WEe只需要提供分区/驱动器名称以及我们要更改的路径或目录即可。 在此示例中,我们将更改为D分区Backups\2019

> cd d:\Backups\2019

通过保存当前路径更改目录 (Change Directory By Saving Current Path)

Navigating the directories in the command line is a very trivial task if there are a lot of different directories to change. pushdcommand can be used to change a new directory by saving the current path to memory. The saved path can be used with the popdcommand which is explained below.

如果要更改许多不同的目录,那么在命令行中导航目录是一项非常琐碎的任务。 通过将当前路径保存到内存,可以使用pushd命令更改新目录。 保存的路径可以与以下说明的popd命令一起使用。

> pushd "c:\Program files"

将目录更改为保存的路径 (Change Directory To The Saved Path)

In the previous example, we have saved the paths and these paths can be easy with the popd command to navigate.

在前面的示例中,我们保存了路径,并且可以使用popd命令轻松浏览这些路径。

> popd
Change Directory To The Saved Path
Change Directory To The Saved Path
将目录更改为保存的路径
LEARN MORE  What Are Inodes In Linux?
了解更多信息Linux中的inode是什么?

翻译自: https://www.poftut.com/how-to-change-directory-in-cmd-ms-dos-and-powershell/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值