On a Linux system, you can open a file by double-clicking it in the file manager or by using a command in the terminal. Knowing how to open a file on Linux is one of the first skills you need, whether you’re a beginner or a seasoned user. This guide covers every method, from graphical clicks to powerful terminal commands, so you can handle any file type with confidence.
Linux offers many ways to open files, each suited for different situations. You might prefer the simplicity of a file manager or the speed of the command line. Let’s explore both worlds step by step.
How To Open A File On Linux
Opening a file in Linux is straightforward once you understand the tools. The method you choose depends on whether you’re in a desktop environment or working remotely via SSH. Below, we break down all the options.
Opening Files With A Graphical File Manager
Most Linux distributions come with a file manager like Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE). These make opening files as simple as clicking.
- Open your file manager from the applications menu or by clicking the folder icon on your desktop.
- Navigate to the directory containing your file.
- Double-click the file. The system automatically opens it with the default application for that file type.
- If you want to use a different program, right-click the file and select “Open With” or “Open With Other Application.”
This method works for text files, images, PDFs, videos, and more. The default application is usually set by your system based on the file extension.
Changing Default Applications
To change which program opens a file type by default, right-click any file of that type, choose “Properties” or “Preferences,” then find the “Open With” tab. Select your preferred application from the list.
Opening Files From The Terminal
The terminal gives you more control and is essential for remote work. Here are the most common commands.
Using The Xdg-Open Command
The xdg-open command opens a file or URL using the system’s default application. It works across different desktop environments.
xdg-open filename.txt
This command opens filename.txt with your default text editor. For images, it opens the image viewer; for PDFs, the PDF reader. It’s the terminal equivalent of double-clicking.
Opening Text Files With Nano Or Vim
For editing text files directly in the terminal, use a text editor like Nano or Vim.
- Nano:
nano filename.txt— opens the file in a simple, user-friendly editor. - Vim:
vim filename.txt— a powerful editor with a steeper learning curve. - Emacs:
emacs filename.txt— another advanced option.
Nano is great for beginners. After opening, you can edit the file, save with Ctrl+O, and exit with Ctrl+X.
Viewing Files With Cat, Less, And More
Sometimes you only need to read a file, not edit it. These commands are perfect for that.
- Cat:
cat filename.txt— prints the entire file content to the terminal. Good for small files. - Less:
less filename.txt— lets you scroll through a file page by page. Pressqto quit. - More:
more filename.txt— similar to less but with fewer features. PressSpaceto scroll.
For large log files, less is your best friend because it doesn’t load the whole file into memory.
Opening Files With Specific Applications
You can launch a specific program from the terminal and tell it to open a file. This is useful when you want to bypass the default application.
gedit filename.txt
libreoffice document.odt
gimp image.png
firefox index.html
Replace gedit with your preferred text editor, libreoffice for office documents, etc. This method works for any GUI application installed on your system.
Opening Files As Root Or With Sudo
Some system files require root permissions to open or edit. Use sudo before the command.
sudo nano /etc/hosts
sudo gedit /etc/fstab
Be careful when editing system files. Mistakes can break your system. Always double-check what you’re doing.
Opening Files In The Background
If you want to open a file and continue using the terminal, append an ampersand (&) to the command.
gedit filename.txt &
This launches the application in the background, freeing up your terminal for other tasks.
Opening Files From The File Manager’s Terminal
Most file managers have a built-in terminal or a “Open in Terminal” option. Right-click inside a folder and select “Open in Terminal.” Then use any of the terminal commands above.
Handling Different File Types
Different file types may require specific tools. Here’s a quick reference.
- Text files (.txt, .md, .log): Use
nano,vim,gedit, orcat. - Images (.jpg, .png, .gif): Use
eog(Eye of GNOME),gimp, orxdg-open. - PDFs (.pdf): Use
evince,okular, orxdg-open. - Videos (.mp4, .avi): Use
vlc,mpv, orxdg-open. - Audio (.mp3, .wav): Use
audacious,rhythmbox, orxdg-open. - Office documents (.docx, .odt): Use
libreofficeorabiword. - Archives (.zip, .tar.gz): Use
file-roller(GUI) orunzip,tar(terminal).
If a file doesn’t open, you may need to install the appropriate application. Use your package manager to install missing software.
Opening Files With Wildcards And Patterns
You can open multiple files at once using wildcards. For example, to open all text files in a directory with Nano:
nano *.txt
This opens each file one after another in Nano. You can also use cat *.txt to display all text files in the terminal.
Opening Files From A Script Or Program
If you’re writing a script, you can use the same commands. For example, a bash script to open a log file:
#!/bin/bash
xdg-open /var/log/syslog
Make the script executable with chmod +x script.sh and run it.
Common Errors And How To Fix Them
Sometimes things don’t work as expected. Here are typical issues and solutions.
- “Command not found”: The application isn’t installed. Install it with
sudo apt install application-name(Debian/Ubuntu) orsudo dnf install application-name(Fedora). - “Permission denied”: You don’t have read permission. Use
sudoor change file permissions withchmod. - “No such file or directory”: Check the file path. Use
lsto list files in the current directory. - File opens in wrong application: Use
xdg-openor specify the application explicitly.
Using Aliases To Speed Up File Opening
You can create aliases in your shell configuration file (like .bashrc or .zshrc) to open files faster.
alias open='xdg-open'
alias edit='nano'
Then you can type open file.txt or edit file.txt instead of longer commands.
Opening Files On Remote Servers
When working on a remote server via SSH, you don’t have a graphical interface. Use terminal-based tools.
- View logs:
less /var/log/syslog - Edit config files:
nano /etc/nginx/nginx.conf - Copy files to local machine:
scp user@server:/path/to/file .
For a more graphical experience, you can use ssh -X to forward X11 applications, but this requires a local X server.
Opening Files With Environment Variables
You can use environment variables to open files in specific directories. For example, $HOME points to your home directory.
xdg-open $HOME/Documents/report.pdf
This is useful in scripts or when you frequently access files in certain locations.
Security Considerations When Opening Files
Always be cautious when opening files from unknown sources. Malware can be disguised as legitimate files. Use file command to check the actual type.
file suspicious_file.exe
This reveals whether it’s really an executable or something else. Never run scripts or binaries from untrusted sources.
Automating File Opening With Cron Jobs
You can schedule file opening tasks using cron. For example, to open a report every morning at 9 AM:
0 9 * * * xdg-open /home/user/Documents/daily_report.pdf
This is advanced but useful for routine tasks.
Opening Files In Different Desktop Environments
Each desktop environment has its own quirks. Here are specifics for popular ones.
- GNOME: Uses Nautilus.
xdg-openworks well. You can also usegnome-open. - KDE: Uses Dolphin.
kioclient exec file.txtis an alternative. - XFCE: Uses Thunar.
exo-open file.txtis available.
These commands are handy if xdg-open doesn’t work correctly.
Opening Files With File Associations
Linux uses MIME types to associate file extensions with applications. You can view and modify these associations.
xdg-mime query default text/plain
This shows the default application for plain text files. To change it:
xdg-mime default gedit.desktop text/plain
This requires the .desktop file for the application, usually found in /usr/share/applications/.
Opening Files From The Command Line With File Managers
You can also open a file manager from the terminal and navigate to a specific directory.
nautilus /home/user/Documents
dolphin /home/user/Pictures
This opens the file manager at the specified location, where you can then double-click files.
Opening Files With Python Or Other Scripts
If you’re programming, you can open files programmatically. In Python:
import os
os.system('xdg-open file.txt')
Or use the subprocess module for more control.
Opening Files On Headless Systems
On systems without a GUI, you’re limited to terminal tools. Use cat, less, nano, or vim. For binary files, use hexdump or xxd.
Opening Files With Drag And Drop
In many file managers, you can drag a file onto an application icon to open it. For example, drag a text file onto the terminal window to insert its path.
Opening Files With Right-Click Menus
Right-clicking a file gives you options like “Open With,” “Open in Terminal,” or “Open with Text Editor.” This is often the fastest method for occasional use.
Opening Files With Keyboard Shortcuts
Most file managers support keyboard shortcuts. For example, Ctrl+O opens a file dialog, or Enter opens the selected file. Learn your file manager’s shortcuts to speed up workflow.
Opening Files With The Find Command
If you don’t know where a file is, use find to locate it, then open it.
find /home -name "report.pdf" -exec xdg-open {} \;
This finds report.pdf in your home directory and opens it.
Opening Files With The Locate Command
The locate command is faster than find but requires a database update.
locate report.pdf | head -1 | xargs xdg-open
This opens the first match.
Opening Files With Grep And Other Filters
You can combine commands to open files containing specific text. For example, to open all text files containing “error”:
grep -l "error" *.txt | xargs nano
This opens each matching file in Nano.
Opening Files With The Open Command (MacOS Style)
Some Linux distributions have an open command similar to macOS. If not, you can create an alias as shown earlier.
Opening Files With The Run Dialog
Press Alt+F2 to open the run dialog. Type the full path to the file, and it opens with the default application.
Opening Files With The Terminal’s Autocomplete
When typing a file path in the terminal, press Tab to autocomplete. This reduces typing errors and speeds up the process.
Opening Files With The History Command
If you’ve opened a file before, use history to find the command, then run it again.
history | grep xdg-open
This shows past commands, which you can reuse.
Opening Files With The Sudoedit Command
For editing system files safely, use sudoedit which creates a temporary copy.
sudoedit /etc/hosts
This opens the file in your default editor with root privileges, and saves changes securely.
Opening Files With The Xdg-Open For URLs
xdg-open also works for URLs. It opens the default web browser.
xdg-open https://example.com
This is useful for scripts that need to open a web page.
Opening Files With The Wslview Command (WSL)
If you’re using Windows Subsystem for Linux (WSL), use wslview to open files with Windows applications.
wslview file.txt
This opens the file in