【问题标题】:How do I open a file with Chrome from the command line?如何从命令行使用 Chrome 打开文件?
【发布时间】:2013-08-27 17:59:07
【问题描述】:

我想从 bash 终端使用 Google Chrome 或 Chromium 打开当前目录中的文件 (index.html)(我使用的是 Linux Mint 15)。命令是什么?我已经尝试了直观的方法,并且做了一些堆栈和谷歌搜索无济于事,奇怪地被忽视了(也许很明显)。提前致谢。

hermes@hades ~/coding/.../public $ google-chrome index.html
google-chrome: command not found
hermes@hades ~/coding/.../public $ google-chromium index.html
google-chromium: command not found

【问题讨论】:

  • 如果将 chrome 设置为默认浏览器,则只需 open index.html。否则,open -a (chrome location) index.html
  • 至少在 Mac 上这应该是公认的答案。
  • @chovy 不幸的是,这与 Mac 无关。
  • 相关:对于那些试图为灯塔目的安装 chromium 的人,必须安装 chromium (v7) 而不是 chromium-browser (v6)

标签: bash google-chrome


【解决方案1】:

只需输入程序名称,后跟文件:

google-chrome {file-path}

例如:

google-chrome ~/index.html

【讨论】:

  • 这对我不起作用...我将使用终端输出编辑上面的内容
  • 好的。我们需要找到您拥有的 chrome。 CD 到/usr/bin 并做一个`find . -name *chrome* 如果你没有得到任何东西,试试find . -name *chromium* 让我知道你得到了什么。
  • google-chrome 不在 /usr/bin 中,chrome 也不在
  • @neaumusic 你试过find / -name *chrom*吗?你都安装了吗?
  • @neaumusic 是的,你必须先安装 chrome。看起来你有一个mac。如果您将 chrome 设置为默认值,您只需 open {file}
【解决方案2】:

试试

 open {filename}

如果它是.html 文件,它应该在您的默认浏览器中打开

【讨论】:

  • 一个跨平台的方法是python -m webbrowser {filename}。对于 linux,xdg-open {filename} 通常可以工作。但同样,只有当 chrome 被设置为该文件类型的默认应用程序时,这才有效。
  • 这适用于使用 chrome 作为默认浏览器的 mac osx xml 文件
【解决方案3】:

bash shell (在 Windows 上),您可以使用:

start myFilename.html

并打开一个文件夹:

explorer "C:\Program Files\Git"

添加以供参考,因为我的搜索也在这里登陆。

【讨论】:

  • 搜索也把我带到了这里,这正是我要找的。​​span>
【解决方案4】:

对于我正在使用的 Mac

open -a 'google chrome' /yourPath

【讨论】:

    【解决方案5】:

    您可以像 chromium-browser path|file 一样搜索 chromium。

    【讨论】:

    • $ chromium-browser -bash: chromium-browser: command not found
    • @chovy 这可能意味着你没有chromium-browser
    【解决方案6】:

    这个解决方案一直对我有用 - open -a "google\ chrome.app" index.html - 其中“google\ chrome.app”是您系统上 chrome 的名称/位置。

    如果 Chrome 是您的默认浏览器,只需 - open index.html

    【讨论】:

    • open -a "google\ chrome.app" --args --arg1=value1 --arg2=value2
    【解决方案7】:

    如果 Chrome 是您的主要浏览器,请使用

    see your_file.html
    

    【讨论】:

      【解决方案8】:

      Chrome 似乎不在您的 $PATH 中。简单的解决方案可能是卸载并重新安装 Chrome,它应该把它放在你的 $PATH 中。那么

      google-chrome [file] 
      

      应该适合你。

      【讨论】:

      • 你的建议奏效了。
      【解决方案9】:

      您可以使用以下终端命令打开文件 (Linux)

      1. 在新标签页中打开google-chrome < filepath >
      2. 在新窗口中打开google-chrome --new-window < filepath >
      3. 以隐身模式打开google-chrome --incognito (--incongnito-mode) < filepath >
      <filepath> = localhost/test/../filename.html
      

      【讨论】:

        【解决方案10】:

        对于 MacOS,在 bash CLI 中使用绝对路径(如果您的 Chrome 安装在 /Applications 文件夹中):

         /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
        

        所以,如果你想打开 CNN 网页:

         /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome  www.cnn.com
        

        【讨论】:

          【解决方案11】:

          Chrome 不是默认浏览器,这在 Windows 10 下对我有用:

          start "New Chrome Window Title" /d "C:\Program Files (x86)\Google\Chrome\Application" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --new-window "file://C:/Folder/SubFolder/Sub Subfolder/thisfile.html"
          

          你需要:

          • 一种将非限定文件名转换为完全限定文件名的方法;
          • 一种将反斜杠转换为正斜杠的方法;
          • 一种将“file://”前缀到 URL 的方法;
          • 识别您在其中找到chrome.exe 的特定目录;
          • 决定是否要保留开关以强制打开新窗口;
          • 以及其他相关的命令行选项。

          这些在.bat.cmd 文件中应该是可行的,使用FOR 命令和SET 命令的文本替换功能;或者在可以从批处理文件等调用的.vbs.ps1 脚本中执行所有这些操作。

          但基本语法看起来不错。

          【讨论】:

            【解决方案12】:

            至少在 Ubuntu 12.04 上,它是 /opt/google/chrome/chrome;我也有一个符号链接到它/usr/bin/google-chrome

            【讨论】:

              【解决方案13】:
              1. 在 bash 或 git CMD 中,确保您位于项目目录/文件夹中
              2. 输入start index.html
              3. 按 Enter。瞧 :-) 你完成了

              这对我有用。希望它也适合你。

              【讨论】:

                【解决方案14】:

                试试

                /opt/google/chrome/google-chrome --allow-file-access-from-files index.html
                

                或者

                /usr/bin/google-chrome --allow-file-access-from-files index.html
                

                --allow-file-access-from-files 会放松一些security settings ,这对于使用本地文件进行测试很有用。

                【讨论】:

                  【解决方案15】:

                  正如其他人所说,解决方案是使用:

                  google-chrome www.google.com
                  

                  您也可以使用--incognito 以隐身模式打开它们:

                  google-chrome --incognito www.google.com
                  

                  请注意,您只需将它们一个接一个地放置即可同时打开多个页面:

                  google-chrome www.google.com www.yahoo.com
                  

                  如果您想从文件中打开它们,请使用命令替换 $() 将其打开并即时处理:

                  google-chrome $(<file)
                  

                  【讨论】:

                    【解决方案16】:

                    我看到的其他东西都没有为我工作,但我后来发现这个工作:

                    explorer.exe index.html

                    【讨论】:

                    • explorer ??
                    • 这对我有用,可能是因为我将 chrome 设置为我的默认浏览器,但不确定。
                    • 好吧,有道理
                    【解决方案17】:

                    试试这个

                    start chrome "file or path"
                    

                    火狐也一样

                    start firefox "file or path"
                    

                    这对我有用。

                    【讨论】:

                      猜你喜欢
                      • 2012-12-30
                      • 2013-04-27
                      • 1970-01-01
                      • 2012-02-12
                      • 2021-09-07
                      • 2013-11-08
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多