xdg-open

desktop utilitiesLinux/Unix
The xdg-open command is one of the most frequently used commands in Linux/Unix-like operating systems. xdg-open Open a file or URL with the default application

Quick Reference

Command Name:

xdg-open

Category:

desktop utilities

Platform:

Linux/Unix

Basic Usage:

xdg-open [options] [arguments]

Common Use Cases

    Syntax

    xdg-open {file|URL}

    Options

    xdg-open is a simple command with very few options:

    Option Description
    --help Display help information and exit
    --manual Display manual page and exit
    --version Display version information and exit

    File Types and Associated Applications

    xdg-open uses the following rules to determine which application to use:

    File Type Typical Application
    URL (http, https, ftp) Default web browser
    Directory File manager
    Text file (.txt, .md, etc.) Text editor
    Image file (.jpg, .png, etc.) Image viewer
    Document (.pdf, .doc, etc.) Document viewer or office suite
    Audio file (.mp3, .wav, etc.) Audio player
    Video file (.mp4, .avi, etc.) Video player

    Examples

    How to Use These Examples

    The examples below show common ways to use the xdg-open command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Open a file with the default application xdg-open document.pdf
    # Open a directory in the file manager xdg-open /home/user/Documents
    # Open a website in the default browser xdg-open https://www.example.com
    # Advanced Examples Advanced
    # Open a file from a script #!/bin/bash xdg-open "$1"
    # Open an image file xdg-open image.jpg # Open a text file xdg-open notes.txt # Open a spreadsheet xdg-open data.xlsx # Open a directory from the current location xdg-open . # Open multiple files in a loop for file in *.pdf; do xdg-open "$file" done # Run in background to avoid waiting for application to close xdg-open video.mp4 &

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `xdg-open` command is a versatile utility in Linux desktop environments that opens files or URLs with their appropriate default application. It's part of the XDG (X Desktop Group) utilities, which are designed to standardize desktop integration across different Linux distributions. **Core Functionality:** 1. **Universal Opener**: xdg-open serves as a universal opener for any file type or URL, delegating to the appropriate application based on the file's MIME type or URL scheme. 2. **Desktop Integration**: It integrates with the desktop environment's file association settings, respecting the user's preferences for which applications should handle specific file types. 3. **Command Line to GUI Bridge**: xdg-open provides a simple way to open graphical applications from command-line scripts or terminals. 4. **Cross-Desktop Compatibility**: It works across different desktop environments (GNOME, KDE, Xfce, etc.) by using the appropriate method for each environment. **Common Use Cases:** 1. **Opening Files from Scripts**: Shell scripts can use xdg-open to open files with their default applications: ```bash xdg-open report.pdf ``` 2. **Opening URLs from Terminal**: Quickly open websites without manually launching a browser: ```bash xdg-open https://example.com ``` 3. **File Manager Integration**: Opening the file manager at a specific location: ```bash xdg-open ~/Documents ``` 4. **Application Launchers**: Custom application launchers or keyboard shortcuts that need to open files. 5. **Development and Testing**: Testing file associations or web links during development. **Technical Details:** 1. **Implementation Strategy**: xdg-open is typically a shell script that detects the current desktop environment and uses the appropriate method for that environment: - GNOME: `gvfs-open` or `gio open` - KDE: `kde-open` or `kde-open5` - Generic X11: Uses mime database and `xdg-mime` 2. **MIME Type Detection**: xdg-open uses the file's MIME type (determined by file extension and/or content) to decide which application should handle it. 3. **Configuration Sources**: It uses the following sources to determine file associations: - User's personal mime database (~/.config/mimeapps.list) - System-wide mime database (/usr/share/applications/mimeinfo.cache) - Desktop environment specific settings 4. **Exit Status**: xdg-open returns 0 on success, and various non-zero values for different error conditions. **Comparison with Related Commands:** 1. **Desktop-Specific Openers**: Each desktop environment typically has its own opener command: - GNOME: `gvfs-open` or `gio open` - KDE: `kde-open` or `kde-open5` - MacOS: `open` - Windows: `start` 2. **Generic X11 Alternatives**: Before xdg-open became standard, various utilities like `gnome-open`, `exo-open`, etc. were used. 3. **Specialized Openers**: Some applications have their own opener commands, such as `firefox` for web pages or `evince` for PDFs. **Limitations and Considerations:** 1. **Desktop Environment Dependency**: xdg-open works best in a standard desktop environment. It may have limited functionality in minimal window managers or headless systems. 2. **Background Execution**: When xdg-open launches an application, it may not return control to the terminal immediately. Using `&` to run it in the background is often helpful: ```bash xdg-open large_video_file.mp4 & ``` 3. **Error Handling**: xdg-open provides minimal feedback when errors occur. For more robust scripts, additional error checking may be necessary. 4. **Security Considerations**: Opening files from untrusted sources can pose security risks, as xdg-open will execute the associated application which might interpret executable content. **Configuration and Customization:** 1. **Changing Default Applications**: Users can change which application opens a particular file type using: - Desktop environment's settings (e.g., "Default Applications" in GNOME) - `xdg-mime` command: ```bash xdg-mime default mypdfviewer.desktop application/pdf ``` - Editing ~/.config/mimeapps.list directly 2. **Debugging Associations**: To check which application will be used for a file: ```bash xdg-mime query default application/pdf ``` **Best Practices:** 1. **Path Handling**: Always use full paths or properly quote file paths to avoid issues with spaces or special characters: ```bash xdg-open "$HOME/Documents/My Report.pdf" ``` 2. **Background Execution**: Run xdg-open in the background when calling from scripts that should continue execution: ```bash xdg-open "$file" & ``` 3. **Error Checking**: For robust scripts, check the exit status: ```bash if ! xdg-open "$file"; then echo "Failed to open $file" fi ``` 4. **URL Encoding**: Ensure URLs are properly encoded when passing them to xdg-open: ```bash xdg-open "https://example.com/search?q=${encoded_query}" ``` **Tips and Tricks:** 1. **Quick File Preview**: Use xdg-open as a quick way to preview files from the terminal without remembering specific viewer applications. 2. **Directory Navigation**: Combine with `find` to quickly navigate to directories: ```bash xdg-open "$(find ~/Documents -name "Project*" -type d | head -1)" ``` 3. **Quick Web Searches**: Create aliases for common web searches: ```bash alias wiki='xdg-open "https://en.wikipedia.org/wiki/Special:Search?search=$1"' ``` 4. **Integration with Terminal File Managers**: Add keybindings in terminal file managers like `ranger` to open files with xdg-open. xdg-open is a simple yet powerful command that bridges the command line and graphical applications, making it easier to work with files in a desktop environment directly from scripts or terminal sessions.

    Related Commands

    These commands are frequently used alongside xdg-open or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the xdg-open command works in different scenarios.

    $ xdg-open
    View All Commands