Windows cmd 上的“touch”相当于什么?

Joy*_*ssa 4 bash shell powershell command

.js我正在尝试通过 PowerShell 命令行创建 JavaScript文件,但是,当我尝试 echo 时,该文件在创建后立即消失。PowerShell 中是否有与 BASH 命令相同的命令$ touch

PowerShell 中的 Echo 尝试

E7NBB.png

JΛY*_*ÐΞV 7

它看起来像您在 PowerShell 中的情况,与 Windows CMD 提示符略有不同。

\n
\n
\n

PowerShell 相当于 Linux 命令$> touch

\n
\n

PowerShell中,它们是CommandCmdlet,与BASH不同的是,有一个特定的Cmdlet用于创建新文件和目录(以及其他实体)。

\n
\n

PowerShell Cmdlet:$> New-Item

\n
\n
在 shell 环境中创建文件时,您(在大多数情况下)可以将BASH$> touch命令替换为PowerShell CmdLet 。 $> New-item
\n

在PowerShell&中创建新项目的工作方式与在 Linux Shell 中的工作方式稍有不同(我在此答案中特别指的是 Bash)。\n例如,Windows PowerShell\ 的 CmdLet可以$> New-item以下项目创建多种类型的项目 \xe2\x80\x94例如,PowerShell 创建两个目录和文件\xe2\x80\x94;在PowerShell中,您需要指定项目类型。要指定项目类型,您将使用 ItemType 标志-ItemType <argument>。伪参数是我添加的占位符。实际-ItemType参数将是File&/or Directory

\n
\n
\n
\n

示例| 新文件:

\n
\n
以下是被视为创建新文件标准的 Cmdlet 格式:
\n
\n    New-Item -Path \'A:\\testing\\one\\two\\three.txt\' -ItemType File\n\n\n
Run Code Online (Sandbox Code Playgroud)\n

three.txt在目录 中 创建文件two,文件的完整路径名是,A:\\testing\\one\\two\\three.txt

\n
\n
\n

示例| 新目录:

\n
\n
CmdLet$> New-item专门用于创建文件和目录。这有点独特,因为 Linux Shell BASH实现了该$> mkdir命令,但它只创建目录,而不创建文件。然而,BASH 证明 SHELL 不必有专门用于创建文件的命令;这并不是说使用文件创建命令没有任何好处。但是,在 Windows 中,您可以而且应该使用New-ItemCmdLet 来创建目录。
\n
\n
以下是使用 PowerShell 时创建目录的方法:
\n
\nNew-Item -Path \'Z:\\sum-directory\' -ItemType Directory\n\n
Run Code Online (Sandbox Code Playgroud)\n

此命令创建一个名为 的新目录sum-directory。该目录位于驱动器盘符中 Z:\\,目录的完整路径名是 Z:\\sum-directory

\n
\n
\n
有关此主题的更多信息,请访问:
\n
\n
https://learn.microsoft.com/en-us/powershell/scripting/samples/working-with-files-and-folders?view=powershell-7.1#creating-files-and-folders
\n
\n
\n
\n
\n


小智 5

您应该使用typeorcopy代替echo.

C:> 输入 nul >> "file.txt"

或者

C:> 复制 nul“file.txt”

此链接有更多信息: https ://www.shellhacks.com/windows-touch-command-equivalent/