How To Open A File Linux – Open Files With Default Applications

Opening a file in Linux often means using a command like xdg-open to launch the default application for that file type. If you are new to Linux, you might wonder How To Open A File Linux without a graphical interface or with one. This guide covers every method, from simple commands to advanced tricks, so you can open any file fast.

Linux offers many ways to open files. You can use the terminal, file managers, or even shortcuts. Each method works for different situations. Let’s start with the basics and move to more advanced options.

Using The Terminal To Open Files

The terminal is a powerful tool. You can open files with just a few keystrokes. Here are the most common commands.

Opening Files With Xdg-Open

xdg-open is the standard command. It opens a file with its default application. For example, a text file opens in your text editor, and a PDF opens in your PDF viewer.

  1. Open your terminal.
  2. Type xdg-open filename. Replace “filename” with your file’s name.
  3. Press Enter. The file opens in the default app.

This command works on most Linux distributions. It is simple and reliable.

Opening Text Files With Nano Or Vim

For text files, you can use terminal editors. nano is beginner-friendly. vim is more advanced but powerful.

  • To open a file with nano: nano filename.txt
  • To open a file with vim: vim filename.txt

Nano shows instructions at the bottom. Vim requires learning modes. Both let you edit and save files directly in the terminal.

Opening Files With Cat Or Less

Sometimes you only need to view a file, not edit it. Use cat to print the entire content to the terminal. Use less to scroll through large files.

  • cat filename.txt shows everything at once.
  • less filename.txt lets you move up and down with arrow keys. Press q to quit.

These commands are fast and use no extra resources.

How To Open A File Linux With A Graphical Interface

If you prefer a desktop environment, you have several options. Most Linux distributions include a file manager.

Using The File Manager

File managers like Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE) work like Windows Explorer or macOS Finder.

  1. Open your file manager from the menu or dock.
  2. Navigate to your file.
  3. Double-click the file. It opens with the default application.

You can also right-click a file. Choose “Open With” to select a different program.

Opening Files From The Desktop

If your file is on the desktop, just double-click it. This works for most file types. If nothing happens, check the file association.

Using The Run Dialog

Press Alt + F2 to open the run dialog. Type the file path and press Enter. The file opens with its default app.

This method is quick if you know the exact path.

Opening Files With Specific Applications

Sometimes you want to open a file with a particular program. Linux makes this easy.

From The Terminal

Type the application name followed by the file name. For example:

  • gedit filename.txt opens a text file in Gedit.
  • libreoffice filename.odt opens a document in LibreOffice.
  • firefox filename.html opens an HTML file in Firefox.

This method gives you full control over which app is used.

From The File Manager

Right-click the file. Select “Open With” or “Properties.” Choose your preferred application. You can set it as the default for that file type.

This is useful when you have multiple apps for the same file type.

Opening Files With Wildcards And Patterns

You can open multiple files at once using wildcards. The asterisk * matches any characters.

  • xdg-open *.txt opens all text files in the current directory.
  • nano file*.txt opens all files starting with “file” in nano.

Be careful with this. Opening many files at once can be overwhelming.

Opening Files With Sudo Or Root Privileges

Some system files require root access. Use sudo before the command.

  • sudo nano /etc/hosts opens the hosts file for editing.
  • sudo xdg-open /var/log/syslog opens a system log.

Always double-check before editing system files. Mistakes can break your system.

Opening Files In Different Directories

You do not need to be in the same directory as the file. Use the full path or relative path.

  • Full path: xdg-open /home/user/Documents/report.pdf
  • Relative path: xdg-open ../Documents/report.pdf

Relative paths start from your current directory. The .. means “parent directory.”

Opening Files With File Associations

Linux uses MIME types to decide which app opens a file. You can change associations.

Checking Current Association

Run xdg-mime query default text/plain to see the default text editor. Replace text/plain with other MIME types like application/pdf.

Changing Association

Use xdg-mime default app.desktop mime/type. For example:

xdg-mime default gedit.desktop text/plain sets Gedit as the default for text files.

This method works across all applications that use MIME types.

Opening Files With Symlinks And Shortcuts

Symlinks are like shortcuts. They point to another file. Opening a symlink opens the target file.

Create a symlink with ln -s /path/to/file linkname. Then open linkname normally.

This is useful for files in deep directories.

Opening Files From The Command Line With Nohup

If you close the terminal, the opened file might close too. Use nohup to prevent this.

nohup xdg-open filename &

The & runs the command in the background. The file stays open even after you close the terminal.

Opening Files With Environment Variables

Environment variables like $HOME or $PWD can shorten paths.

  • xdg-open $HOME/Documents/file.txt
  • xdg-open $PWD/file.txt

This is handy in scripts or when you work in many directories.

Opening Files With Guake Or Drop-Down Terminals

Drop-down terminals like Guake let you open files quickly. Press a hotkey, type the command, and press the hotkey again to hide the terminal.

This method keeps your workspace clean.

Opening Files With Aliases

Create aliases for frequently used commands. Add this to your .bashrc or .zshrc file:

alias open='xdg-open'

Now you can type open filename instead of xdg-open filename.

Aliases save time and keystrokes.

Opening Files With Scripts

Write a script to open files in a specific way. For example, a script that opens all PDFs in a folder:

#!/bin/bash
for file in *.pdf; do
xdg-open "$file"
done

Save it, make it executable with chmod +x script.sh, and run it.

Opening Files With Drag And Drop

In graphical environments, you can drag a file onto an application icon. The app opens the file.

This works with text editors, image viewers, and more.

Opening Files With Keyboard Shortcuts

Some file managers let you assign keyboard shortcuts. For example, pressing Ctrl + O opens a file dialog. You can also set custom shortcuts in your desktop environment settings.

Opening Files With Thumbnail Previews

File managers often show thumbnails for images and PDFs. Clicking the thumbnail opens the file. This is faster than reading file names.

Opening Files With Remote Access

If the file is on a remote server, use SSH or FTP. Open it with ssh user@server 'xdg-open /path/to/file' or mount the remote directory.

This requires network access and proper permissions.

Opening Files With AppImage Or Portable Apps

Some applications come as AppImage files. Make them executable with chmod +x app.AppImage, then double-click or run from terminal.

These apps do not need installation.

Opening Files With Flatpak Or Snap

Flatpak and Snap packages run in sandboxes. Open files with flatpak run app.name filename or snap run app.name filename.

These commands ensure the app has access to the file.

Opening Files With Wine Or Windows Apps

If you use Wine to run Windows programs, open files with wine program.exe filename. The Windows app handles the file.

This is useful for legacy software.

Opening Files With Binary Or Hex Editors

For binary files, use hexdump or xxd to view raw data. Open with hexdump filename | less or xxd filename.

These tools show the file’s structure.

Opening Files With Image Viewers

Image viewers like Eye of GNOME (eog) or feh open image files. Use eog image.jpg or feh image.png.

They support slideshows and zoom.

Opening Files With Video Players

Video players like VLC or MPV open media files. Use vlc video.mp4 or mpv video.mkv.

These players handle most formats.

Opening Files With Audio Players

Audio players like Audacious or Rhythmbox open music files. Use audacious song.mp3 or rhythmbox song.flac.

They manage playlists and libraries.

Opening Files With Archive Managers

Archive managers like File Roller or Ark open compressed files. Use file-roller archive.zip or ark archive.tar.gz.

They extract or browse contents.

Opening Files With Web Browsers

Web browsers open HTML, PDF, and image files. Use firefox file.html or chromium file.pdf.

They render web content and documents.

Opening Files With Office Suites

LibreOffice opens documents, spreadsheets, and presentations. Use libreoffice document.odt or libreoffice --calc spreadsheet.ods.

It is a full office suite.

Opening Files With Code Editors

Code editors like VS Code or Atom open source files. Use code script.py or atom index.html.

They offer syntax highlighting and plugins.

Opening Files With Terminal Multiplexers

Terminal multiplexers like tmux or screen let you open files in separate panes. Open a file in one pane and run commands in another.

This improves productivity.

Opening Files With File Managers In Terminal

Terminal file managers like Midnight Commander (mc) or Ranger let you browse and open files. Use arrow keys to navigate and Enter to open.

They combine terminal speed with visual browsing.

Opening Files With Grep And Find

If you do not know the file name, use find to locate it, then open it. Example:

find /home -name "*.txt" -exec xdg-open {} \;

This finds and opens all text files in your home directory.

Opening Files With History And Recent Files

Most file managers show recent files. Click on them to open. The terminal also remembers commands. Press Ctrl + R and type part of the command to recall it.

Opening Files With Drag To Terminal

Drag a file from the file manager into the terminal. The file path appears. Add a command before it and press Enter.

This saves typing.

Opening Files With Copy And Paste Path

Copy a file path from the file manager (Ctrl + C) and paste it in the terminal after a command.

This is accurate and fast.

Opening Files With Tab Completion

In the terminal, type part of the file name and press Tab. The shell completes the name. Press Tab twice to see all matches.

This reduces errors.

Opening Files With Redirection

You can open a file by redirecting its content to a command. For example, cat file.txt | less shows the file in less.

This is useful for processing.

Opening Files With Process Substitution

Use process substitution to open a file as if it were a command output. Example: diff <(cat file1.txt) <(cat file2.txt).

This is advanced but powerful.

Opening Files With Here Documents

You can create and open a file on the fly. Use cat > newfile.txt << EOF, type content, then EOF. Then open it normally.

This is good for quick notes.

Opening Files With File Descriptors

In scripts, you can open files with file descriptors. Example: exec 3< file.txt opens file.txt for reading on descriptor 3.

This is for advanced scripting.

Opening Files With Inotify

Use inotifywait to monitor a file and open it when changed. Example: inotifywait -e modify file.txt && xdg-open file.txt.

This automates workflows.

Opening Files With Cron Jobs

Schedule a cron job to open a file at a specific time. Add a line like 0 9 * * * xdg-open /path/to/file to your crontab.

This opens the file every day at 9 AM.

Opening Files With Systemd Services

Create a systemd service to open a file on boot. Write a service file that runs xdg-open and enable it.

This is for persistent setups.

Opening Files With Desktop Entries

Desktop entries (.desktop files) define how to open files. You can create custom entries for specific tasks.

This integrates with your menu.