touch 命令不起作用,我应该改用啥? [复制]

Posted

技术标签:

【中文标题】touch 命令不起作用,我应该改用啥? [复制]【英文标题】:touch command not working, what should I use instead? [duplicate]touch 命令不起作用,我应该改用什么? [复制] 【发布时间】:2021-08-12 01:06:12 【问题描述】:

我正在尝试在 vs-code 中使用终端创建一个空文件,但命令行没有心情工作。不明白怎么回事?

这是我尝试制作文件时的错误:

touch : The term 'touch' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path 
was included, verify that the path is correct and try again.
At line:1 char:1
+ touch app.js
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (touch:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 

【问题讨论】:

你从哪里得到touch这个词?你想做什么 如果你想要一个函数,你可以在 PowerShell 中调用 wityh touch,也许 this 就是你要找的? 我正在尝试创建一个空文件 然后看看New-Item cmdlet。 @NeelKotkar - touch.exe 不是本机 windows 命令,也不是内置 windows 实用程序。获得这样的唯一方法是 [A] 安装该 util [git-for-windows 安装这样做] [B] 创建一个类似的 util 作为一个函数。 【参考方案1】:

正如Lee Dailey 指出的那样,touch 既不是 PowerShell 命令也不是标准实用程序(外部程序)在 Windows 上;相比之下,在类 Unix 平台上touch is a standard utility 具有双重用途:

(a) 当给定 现有 文件路径时,文件的最后修改 (.LastWriteTime) 和最后访问 (.LastAccessTime) 时间戳设置为当前时间点,由默认。

(b) 当给定不存在的文件路径时,默认情况下会创建此类文件

在PowerShell中没有等效命令(从PowerShell 7.2开始),但是你可以使用现有的命令分别实现(a)和(b),你可以编写自定义脚本或函数它以类似于 Unix touch 实用程序的方式提供 (a) 和 (b):

(a) 的 PowerShell 实现,通过 Get-ItemSystem.IO.FileInfo 实例的属性:
# Update the last-modified and last-accessed timestamps of existing file app.js
$file = Get-Item -LiteralPath app.js
$file.LastWriteTime = $file.LastAccessTime = Get-Date
(b)的PowerShell实现,使用New-Item cmdlet:
# Create file app.js in the current dir.
# * If such a file already exists, an error will occur.
# * If you specify -Force, no error will occur, but the existing file will be 
# *truncated* (reset to an empty file).
$file = New-Item app.js

注意:创建文件时,默认为0-byte文件,但可以通过-Value参数传递内容。


Jeroen Mostert 建议使用以下技术来实现 条件 性质 touch 实用程序的文件创建:

# * If app.js exists, leaves it untouched (timestamps are *not* updated)
# * Otherwise, create it.
Add-Content app.js $null

注意:

要获得类似touch 的行为,(a)(更新最后写入和最后访问的时间戳)仍必须单独实现。

至少假设Add-Content 方法可能失败,即如果现有目标文件是只读 - 而操作此类文件的时间戳 em> 可能仍然有效。 (虽然您可以抑制错误,但这样做可能会让您错过真正的失败。)

下一节指向一个自定义函数,它包含 (a) 和 (b),同时还提供 Unix touch 实用程序提供的附加功能。


一个名为 Touch-File自定义函数,它实现了 Unix touch 实用程序的大部分功能 - 它有几个选项可以提供超出所描述的默认值的其他行为以上 - 可从this MIT-licensed Gist获得。 假设您已经查看了链接代码以确保它是安全的(我可以亲自向您保证,但您应该始终检查),您可以直接下载并在当前会话中定义它,如下所示,它还提供了有关如何使其在以后的会话中可用:

irm https://gist.github.com/mklement0/82ed8e73bb1d17c5ff7b57d958db2872/raw/Touch-File.ps1 | iex

注意:Touch 不是 PowerShell 中的 approved verb,但它仍然被选中, 因为没有一个被批准的动词可以充分传达核心功能 这个命令。

示例调用:

# * If app.js exists, update its last-write timestamp to now.
# * Otherwise, create it (as an empty file).
Touch-File app.js

【讨论】:

【参考方案2】:

怎么样?

Out-File -FilePath "app.js"

【讨论】:

你能提供更多的上下文吗?这是否绝对可以以完全相同的方式代替touch 其实不是。 linux touch 命令如果不存在则创建文件,如果存在则更新时间戳。它可能需要编写一种触摸文件函数来模仿这个作为输出文件,这里使用的只是创建一个空文件。 Windows PowerShell(最高 v5.1,Windows 附带的仅限 Windows 的旧版)中,生成的文件不会是:因为Out-File file默认为“Unicode”编码(UTF-16LE),所以文件是使用UTF-16LE BOM(字节顺序标记)创建的,即使用以下2 个字节0xFF 0xFE$null > file 同上,它实际上是 Out-File 的别名。对于真正的空文件,请使用 Set-Content file $nullNew-Item file。相比之下,跨平台 PowerShell (Core) 版本 (v6+) 始终使用无 BOM 的 UTF-8,因此 Out-File / $null > 也可以在那里工作。

以上是关于touch 命令不起作用,我应该改用啥? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

httpClient.getConnectionManager() 已弃用-应该改用啥?

OS X 10.5 SDK 已弃用 getAttributeNS;我应该改用啥?

java.net.URLEncoder.encode(String) 已弃用,我应该改用啥?

Sencha Touch cmd - 不同的 sencha.cfg 文件有啥作用?

为啥这个 bash expr 命令不起作用? [复制]

构建后复制命令不起作用 (.vbproj)