How to Use the ‘open’ Command in Linux

Unlock the power of Linux's 'open' command for easy file and URL management.

The open command in Linux serves as a gateway to access files, directories, and URLs from the terminal. It’s a versatile tool that allows users to launch files and applications with ease. Similar to the open command, commands like xdg-open in Linux or start in Windows perform comparable functions, bridging the gap between the command-line interface and graphical user interface.

Primarily used by system admins, developers, and power users, the open command can be a vital part of daily tasks. Whether it’s opening a text file for editing or launching a web page for quick access, this command simplifies the process. For those looking to enhance their workflow, combining the open command with other commands like ls for listing files or grep for searching can create a powerful toolkit.

How to Install the open Command

The open command is typically included by default in macOS, so there’s no need to install anything if you’re using that operating system. However, if you’re using a different Unix-based system like Linux, the open command might not be available, and you may need to use a similar command like xdg-open.

If you still want to use a command named open on a Linux system, you could create an alias for xdg-open. Here’s how you can do that:

  1. Open your shell profile file (e.g., ~/.bashrc or ~/.bash_profile) in a text editor.
  2. Add the following line: alias open='xdg-open'.
  3. Save the file and run source ~/.bashrc (or the appropriate profile file) to apply the changes.

To uninstall or remove this alias, you would simply:

  1. Open the profile file again.
  2. Remove the line alias open='xdg-open'.
  3. Save the file and run source ~/.bashrc (or the appropriate profile file) to apply the changes.

How to Use open

1. Opening a File with the Default Application

Syntax: open <filename>

Explanation: Opens a file with the default application associated with its file type.

Example: open document.txt

The file document.txt is opened in the default text editor, such as TextEdit on macOS.

2. Opening a URL in the Default Web Browser

Syntax: open <URL>

Explanation: Opens a URL in the default web browser.

Example: open http://www.example.com

The URL http://www.example.com is opened in the default web browser, such as Safari on macOS.

3. Opening a File with a Specific Application

Syntax: open -a <application> <filename>

Explanation: Opens a file with a specific application.

Example: open -a 'Google Chrome' index.html

The file index.html is opened in Google Chrome, rather than the default web browser or text editor.

4. Opening a File with a Specific Application Using Bundle Identifier

Syntax: open -b <bundle-identifier> <filename>

Explanation: Opens a file with a specific application using the application’s bundle identifier.

Example: open -b com.apple.Preview image.png

The file image.png is opened in Preview, identified by its bundle identifier com.apple.Preview.

5. Opening a New Terminal Window (macOS)

Syntax: open -a Terminal

Explanation: Opens a new Terminal window on macOS.

Example: open -a Terminal

A new Terminal window is opened, allowing for a separate command-line session.

6. Opening a File and Bringing the Application to the Foreground

Syntax: open -g <filename>

Explanation: Opens a file and brings the application to the foreground.

Example: open -g document.txt

The file document.txt is opened in the default text editor, and the application window is brought to the foreground.

7. Opening a File with a Fresh Application Instance

Syntax: open -n <filename>

Explanation: Opens a file in a new instance of the default application, even if the application is already running.

Example: open -n document.txt

The file document.txt is opened in a new instance of the default text editor, allowing for multiple instances of the application to run simultaneously.

8. Showing the File in Finder Without Opening (macOS)

Syntax: open -R <filename>

Explanation: Reveals the file in Finder without opening it.

Example: open -R document.txt

The file document.txt is highlighted in Finder, allowing the user to see its location without opening the file itself.

9. Opening a Directory in the Finder (macOS)

Syntax: open <directory>

Explanation: Opens a directory in the Finder on macOS.

Example: open /Users/username/Documents

The directory /Users/username/Documents is opened in the Finder, displaying its contents.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail