touch 命令不起作用,我应该使用什么? [英] touch command not working, what should I use instead?

查看:237
本文介绍了touch 命令不起作用,我应该使用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create an empty file using terminal in vs-code but the command line isn't in the mood to work. I don't understand whats wrong?

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

This is the error when I try to make the file:

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
 

推荐答案

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

As Lee Dailey points out, touch is neither a PowerShell command nor a standard utility (external program) on Windows; by contrast, on Unix-like platforms touch is a standard utility with dual purpose:

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

  • (a) When given existing file paths, the files' last-modified (.LastWriteTime) and last-accessed (.LastAccessTime) timestamps are set to the current point in time, by default.

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

(b) When given non-existing file paths, such files are created, by default.

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

There is no equivalent command in PowerShell (as of PowerShell 7.2), but you can use existing commands to implement (a) and (b) separately, and you can write a custom script or function that provides both (a) and (b) in a manner similar to the Unix touch utility:

# 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字节的文件,但您可以通过-Value参数传递内容.

    Note: When a file is created, it is a 0-byte file by default, but you may pass content via the -Value parameter.

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

    Jeroen Mostert suggests the following technique to implement the conditional nature of the touch utility's file creation:

    # * If app.js exists, leaves it untouched.
    # * Otherwise, create it.
    Add-Content app.js $null
    

    注意:

    • 要获得类似 touch 的行为,(a) 仍必须单独实现.

    • To get touch-like behavior, (a) must still be implemented separately.

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

    At least hypothetically the Add-Content approach can fail, namely if an existing target file is read-only - whereas manipulating such a file's timestamps may still work. (While you could suppress the error, doing so could make you miss true failures.)

    下一节将介绍一个自定义函数,它同时封装了 (a) 和 (b),同时还提供了 Unix touch 实用程序提供的附加功能.

    The next section points to a custom function that wraps both (a) and (b), while also providing additional functionality that the Unix touch utility offers.

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

    A custom function named Touch-File that implements most of the functionality of the Unix touch utility - which has several options to provide additional behaviors beyond the defaults described above - is available from this MIT-licensed Gist.
    Assuming you have looked at the linked code to ensure that it is safe (which I can personally assure you of, but you should always check), you can directly download and define it the current session as follows, which also provides guidance on how to make it available in future sessions:

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

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

    Note: Touch is not an approved verb in PowerShell, but it was chosen nonetheless, because none of the approved verbs can adequately convey the core functionality of this command.

    示例调用:

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

    这篇关于touch 命令不起作用,我应该使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆