对于大多数Mac用户来说,打开文件或文件夹的首选方式是通过Finder 应用程序(Finder app)。但这不是您导航和访问文件或文件夹系统的唯一方法——您可以通过命令行。终端(Terminal)是该命令行的默认网关,它不需要您指向或单击任何东西——只需键入一个命令,您的Mac就会执行该命令。
在访问文件或文件夹时,终端(Terminal)会在多种情况下派上用场。例如,如果您是开发人员或命令行用户并使用Terminal。当 macOS 是服务器软件而终端(Terminal)是导航操作系统的唯一方式时,该应用程序也很方便。
在本指南中,我们将向您展示如何在Mac上的终端(Terminal)中打开文件或文件夹。
如何在终端中打开文件(How to Open a File in Terminal)
您可以使用终端(Terminal)通过其默认/特定应用程序或文本编辑器打开文件。
当您打开Terminal时,您会看到如下内容:
Last login: Mon Sep 6 17:03:20 on ttys000beeagey@Elsiers-Air ~ %
通过其默认应用程序在终端中打开文件(Open a File in Terminal Through Its Default Application)
您可以使用文件的默认应用程序打开文件。
- 选择前往(Go)>实用程序(Utilities)。
- 接下来,选择终端(Terminal)以打开应用程序。
- 右键单击该文件并选择Copy。或者,您可以将文件拖到终端(Terminal)窗口以添加到命令行。
- 在终端窗口中,键入Open [您复制的文件路径](Open [file pathway you copied])并点击Return按钮以使命令生效。
注意(Note):如果您在命令路径中出错,您将收到“不存在此类文件或目录”消息。
使用特定应用程序在终端中打开文件(Open a File in Terminal Using a Specific Application)
如果您想使用您指定的应用程序在终端(Terminal)中打开文件,您仍然需要您之前复制的文件路径和您正在使用的应用程序的名称。
- 在终端(Terminal)窗口中,键入 Open -a “App Name” [文件路径]。例如,您可以输入Open -a “Preview” /Users/beeagey/Desktop/GOOD\ OR\ BAD\ HUSTLE.jpg。
- 按下Return按钮以使命令生效。
使用文本编辑器在终端中打开文件(Open a File in Terminal Using the Text Editor)
您可以使用 Mac 的文本编辑器通过终端打开文件并编辑在(Terminal)Word或OpenOffice和其他文字处理应用程序中创建的富文本文档。
- 打开终端(Terminal)并输入以下命令:打开-a 文本编辑 [(-a Text Edit [)文件路径]。例如:Open -a TextEdit /Users/beeagey/Desktop/How\ to\ Open\ a\ File\ or\ Folder\ in\ Terminal\ on\ Mac.docx。
- Presst Return运行命令并打开文件。
如何在终端中打开文件夹(How to Open a Folder in Terminal)
您可以在终端(Terminal)中打开一个文件夹以查看文件夹中的内容。
- 选择前往(Go)>实用程序(Utilities)>终端(Terminal)。
- 输入(Enter)open /path/to/Directory 语法以打开文件夹。例如,您可以键入Open /Users/beeagey/Desktop/screenies。
注意(Note):如果您输入了错误的文件夹路径,例如/Users/beeagey/screenies,您将收到如下消息:
The file /Users/beeagey/screenies does not exist.
beeagey@Elsiers-Air ~ %
您还可以使用以下几个其他命令访问特定文件夹。
- 根目录:Open /。
- 主文件夹:Open ~。要获取~符号,请按Shift + N。
- Finder 中的当前工作文件夹:打开 . (Open .).
- 在不使用 Finder 的情况下打开应用程序:Open /Applications/nameofapplication.app。例如,如果要打开Safari,请输入Open /Applications/Safari.app。
使用快捷菜单在终端中打开文件夹(Open a Folder in Terminal Using a Shortcut Menu)
如果您想使Finder目录成为当前工作目录,您可以使用快捷(shortcut)菜单扭转这种情况。
- 选择Apple 菜单(Apple menu)>系统偏好设置(System Preferences)。
- 接下来,选择键盘(Keyboard)。
- 转到快捷方式(Shortcuts)选项卡。
- 选择服务(Services)。
- 在文件夹(New Terminal at Folder)选项中找到新终端。
- 在Finder(Finder)中选择一个文件夹,打开服务(Services)菜单并在文件夹中选择(Folder)新建终端(New Terminal)。每当您想在Terminal(Terminal)和GUI之间切换时,您都会这样做。
控制在 Mac 上打开文件和文件夹的方式(Control How You Open Files and Folders on Your Mac)
您可以在Mac上使用(Mac)终端(Terminal)执行各种不同的任务。其中一些可以在Finder中完成,但通过(Finder)Terminal时会快得多。您还可以访问 macOS 的根深蒂固的部分,如果没有专业应用程序 ,您将无法从Finder访问这些部分。(Finder)
查看更多在 macOS 上使用终端终止进程(kill a process using Terminal on macOS)、(Terminal)识别网络设置(identify network settings)或更新 Mac 应用程序(update Mac apps)的方法。
(Was)本指南有帮助吗?在评论中分享(Share)您的想法。
How to Open a File or Folder in Terminal on Mac
For most Mac users, the preferred way of opеning files or folders is through the Finder app. But that’s not the only way you can navigate and access the file or folder system – you can go through a command line. Terminal is the default gateway to that command line, which doesn’t require you to point or click anything – just type a command and your Mac and it will execute the command.
There are several situations where Terminal comes in handy when accessing a file or folder. For instance, if you’re a developer or command-line user and work with Terminal. The app also comes in handy where macOS is the server software and Terminal is the only way to navigate the operating system.
In this guide, we’ll show you how to open a file or folder in Terminal on your Mac.
How to Open a File in Terminal
You can use Terminal to open a file through its default/specific app or through the text editor.
When you open Terminal, you’ll see something like this:
Last login: Mon Sep 6 17:03:20 on ttys000
beeagey@Elsiers-Air ~ %
Open a File in Terminal Through Its Default Application
You can open a file using the file’s default application.
- Select Go > Utilities.
- Next, select Terminal to open the application.
- Right-click the file and select Copy. Alternatively, you can drag the file to the Terminal window to add to the command line.
- In the Terminal window, type Open [file pathway you copied] and hit the Return button for the command to take hold.
Note: If you make a mistake in your command pathway, you’ll get the “No such file or directory exists” message.
Open a File in Terminal Using a Specific Application
If you want to open your file in Terminal using an application you specify, you’ll still need the file pathway that you copied earlier and the name of the app you’re using.
- In the Terminal window, type Open -a “App Name” [file pathway]. For example, you can enter Open -a “Preview” /Users/beeagey/Desktop/GOOD\ OR\ BAD\ HUSTLE.jpg.
- Press the Return button for the command to take hold.
Open a File in Terminal Using the Text Editor
You can use your Mac’s text editor to open files via Terminal and edit rich text documents created in Word or OpenOffice and other word processing apps.
- Open Terminal and enter this command: Open -a Text Edit [file pathway]. For example: Open -a TextEdit /Users/beeagey/Desktop/How\ to\ Open\ a\ File\ or\ Folder\ in\ Terminal\ on\ Mac.docx.
- Presst Return run the command and open the file.
How to Open a Folder in Terminal
You can open a folder in Terminal to see what’s in the folder.
- Select Go > Utilities > Terminal.
- Enter open /path/to/Directory syntax to open the folder. For example, you can type Open /Users/beeagey/Desktop/screenies.
Note: If you enter the wrong folder pathway, like /Users/beeagey/screenies, you’ll get a message like the one below:
The file /Users/beeagey/screenies does not exist.
beeagey@Elsiers-Air ~ %
You can also access specific folders using several other commands as follows.
- Root directory: Open /.
- Home folder: Open ~. To get the ~ symbol, press Shift + N.
- Current working folder in Finder: Open . .
- Open an app without using Finder: Open /Applications/nameofapplication.app. For example, if you want to open Safari, enter Open /Applications/Safari.app.
Open a Folder in Terminal Using a Shortcut Menu
If you want to make a Finder directory the current working directory, you can reverse the situation using a shortcut menu.
- Select Apple menu > System Preferences.
- Next, select Keyboard.
- Go to the Shortcuts tab.
- Select Services.
- Find the New Terminal at Folder option.
- Select a folder within Finder, open the Services menu and select New Terminal at Folder. You’ll be doing this whenever you want to switch between Terminal and a GUI.
Control How You Open Files and Folders on Your Mac
You can use Terminal for all sorts of different tasks on your Mac. Some of them can be done in Finder but it’s much faster when you go through Terminal. You can also access deep-rooted parts of macOS that you wouldn’t access from Finder without specialist apps.
Check out more ways to use Terminal to kill a process using Terminal on macOS, identify network settings, or update Mac apps.
Was this guide helpful? Share your thoughts in a comment.