The Linux terminal offers several commands to open a file, with `xdg-open` being the simplest way to launch it in its default application. If you’re wondering how to open file in terminal linux, you’ve come to the right place. This guide covers every major method, from quick one-liners to advanced text-based tools, so you can pick the best approach for your workflow.
Whether you’re a beginner or a seasoned user, knowing how to open files from the command line saves time and boosts productivity. Let’s jump right in.
How To Open File In Terminal Linux
The most direct way to open a file in Linux terminal is using the `xdg-open` command. It automatically detects the file type and launches the associated default application—like a PDF viewer for .pdf files or a text editor for .txt files.
Here’s the basic syntax:
xdg-open filename.ext
For example, to open a PDF named “report.pdf”:
xdg-open report.pdf
This command works on most Linux distributions, including Ubuntu, Fedora, and Debian. It’s part of the `xdg-utils` package, which is usually pre-installed. If not, install it with:
sudo apt install xdg-utils # Debian/Ubuntu
sudo dnf install xdg-utils # Fedora
One caveat: `xdg-open` runs in the background, so the terminal prompt returns immediately. This is handy for graphical files but less useful if you want to edit a text file directly in the terminal.
Opening Files With Text Editors In The Terminal
For editing configuration files, scripts, or logs, you’ll want a terminal-based text editor. Here are the most common ones:
- nano – Beginner-friendly, with on-screen shortcuts
- vim – Powerful but has a steep learning curve
- emacs – Highly extensible, can be used in terminal mode
- micro – Modern, with familiar keybindings (Ctrl+S to save)
To open a file with nano:
nano myfile.txt
To open with vim:
vim myfile.txt
If the file doesn’t exist, these editors will create a new one when you save. This is perfect for quick edits without leaving the terminal.
Using Nano For Quick Edits
Nano is ideal for beginners because it shows commands at the bottom of the screen. To open a file:
- Type
nano filenameand press Enter - Edit the content using arrow keys
- Press
Ctrl+Oto save, thenCtrl+Xto exit
Nano supports syntax highlighting for many file types, making it easier to read code. It’s pre-installed on most systems.
Using Vim For Power Users
Vim is legendary but intimidating at first. To open a file:
- Type
vim filename - Press
ito enter insert mode and start editing - Press
Escto exit insert mode - Type
:wqto save and quit, or:q!to quit without saving
Vim’s modal editing takes practice, but it’s incredibly efficient once mastered. For quick file viewing without editing, use vim filename and press q to quit.
Viewing Files Without Editing
Sometimes you just need to read a file, not change it. These commands are perfect for that:
cat filename– Prints entire file to terminalless filename– Scrollable viewer, great for long fileshead filename– Shows first 10 lines by defaulttail filename– Shows last 10 lines by default
For example, to view a log file page by page:
less /var/log/syslog
Use Space to scroll down, b to go back, and q to quit. The less command is efficient even with huge files because it doesn’t load the entire file into memory.
Using Cat For Small Files
cat is fine for short files but overwhelms the terminal with long ones. It’s also useful for concatenating multiple files:
cat file1.txt file2.txt > combined.txt
This creates a new file with the contents of both. Not exactly “opening,” but handy.
Opening Files With Specific Applications
If you want to open a file with a particular program from the terminal, you can specify the application directly:
firefox index.html
gedit notes.txt
libreoffice --writer report.odt
This works because many GUI applications accept filenames as arguments. For image files, try eog image.png (Eye of GNOME) or gimp photo.jpg.
To find out which application handles a file type, use xdg-mime query default followed by the MIME type:
xdg-mime query default text/plain
This returns the default application’s .desktop file name, like gedit.desktop.
Opening Files In The Background
To keep your terminal free after opening a GUI application, append an ampersand (&) to the command:
gedit notes.txt &
This runs the process in the background, so you can continue typing commands. You can also use nohup to prevent the process from closing when you exit the terminal:
nohup libreoffice --writer report.odt &
This is useful for long-running tasks like document conversion.
Opening Files With File Managers
You can also launch the default file manager from the terminal to browse files graphically:
nautilus . # GNOME
dolphin . # KDE
thunar . # XFCE
The dot (.) represents the current directory. Replace it with a specific path to open that folder. This is a quick way to navigate visually without leaving the terminal.
Advanced File Opening Techniques
Once you’re comfortable with basic methods, these advanced techniques can streamline your workflow.
Using Aliases For Frequently Opened Files
Create shortcuts in your shell configuration file (e.g., ~/.bashrc or ~/.zshrc):
alias notes='xdg-open ~/Documents/notes.txt'
alias config='nano ~/.bashrc'
After reloading the config with source ~/.bashrc, you can type notes to open your notes file instantly.
Opening Files By Extension With A Function
Add this function to your shell config to open files based on their extension:
open() {
case "$1" in
*.txt|*.md) nano "$1" ;;
*.pdf) xdg-open "$1" ;;
*.jpg|*.png) eog "$1" ;;
*) xdg-open "$1" ;;
esac
}
Now you can type open file.txt and it will launch nano automatically.
Using Find And Open Together
Combine find with xdg-open to locate and open a file in one command:
find ~/Documents -name "*.pdf" -exec xdg-open {} \;
This searches for all PDFs in your Documents folder and opens each one. Be careful—this can open many files at once!
Opening Files With Wildcards
Use wildcards to open multiple files at once:
xdg-open *.txt
This opens all .txt files in the current directory with their default applications. For text editors, you can do:
nano *.txt
Nano will open the first file; you can switch between them using Ctrl+X and then n to open the next one.
Common File Opening Scenarios
Here are practical examples you’ll encounter daily.
Opening A Configuration File
System config files often require root privileges. Use sudo with a text editor:
sudo nano /etc/nginx/nginx.conf
Always be careful editing system files—a typo can break services. Make a backup first with cp.
Opening A Log File In Real-Time
To monitor a log file as it updates, use tail -f:
tail -f /var/log/syslog
This shows new lines as they’re written. Press Ctrl+C to stop.
Opening A File With A Specific Line Number
In vim, you can jump to a specific line when opening:
vim +42 script.py
This opens the file and places the cursor on line 42. In nano, you can’t do this directly, but you can use Ctrl+_ and type the line number after opening.
Opening A File In A New Terminal Tab
If your terminal emulator supports tabs, you can open a file in a new tab:
gnome-terminal --tab -- nano file.txt
This is useful for multitasking without cluttering your workspace.
Troubleshooting Common Issues
Even simple commands can fail. Here’s how to fix common problems.
Command Not Found
If you get “command not found” for xdg-open, install the package as shown earlier. For editors like nano, install them with:
sudo apt install nano
Permission Denied
If you can’t open a file, check permissions with ls -l. Use sudo to open system files, but be cautious. For your own files, change permissions with chmod:
chmod +r filename
File Opens In Wrong Application
To change the default application, use xdg-mime default:
xdg-mime default gedit.desktop text/plain
This sets gedit as the default for text files. Find the correct .desktop file in /usr/share/applications/.
No Output From Xdg-Open
If xdg-open seems to do nothing, it might be running but the application failed. Try running it in the foreground to see error messages:
xdg-open file.pdf 2>&1
Common causes: missing application for the file type, or a broken MIME database. Reinstall xdg-utils or update the MIME database with update-mime-database.
Frequently Asked Questions
Q: How do I open a file in terminal Linux without a GUI?
A: Use a terminal-based text editor like nano or vim. For viewing only, use less or cat.
Q: What is the difference between xdg-open and gnome-open?
A: xdg-open works across desktop environments, while gnome-open is GNOME-specific. Use xdg-open for portability.
Q: Can I open a file in terminal Linux with a specific program?
A: Yes, just type the program name followed by the filename, e.g., firefox index.html.
Q: How do I open a file in terminal Linux and edit it?
A: Use a text editor like nano filename or vim filename. Make your changes, then save and exit.
Q: How to open file in terminal Linux with root privileges?
A: Prepend sudo to the command, e.g., sudo nano /etc/hosts. Be careful with system files.
Summary Of Commands
Here’s a quick reference table for opening files in Linux terminal:
| Command | Use Case |
|---|---|
xdg-open file |
Open with default GUI app |
nano file |
Edit in terminal |
vim file |
Advanced editing |
less file |
View long files |
cat file |
View short files |
tail -f file |
Monitor live logs |
program file |
Open with specific app |
Mastering these commands will make you more efficient in the Linux terminal. Start with xdg-open for graphical files, then explore nano for quick edits. As you grow comfortable, try vim for its power and speed.
Remember, the terminal is your friend. Every command you learn reduces your reliance on the mouse and speeds up your workflow. Practice opening different file types, and soon you’ll wonder how you ever managed without these tools.
If you run into trouble, the man command is your best friend—type man xdg-open or man nano for detailed documentation. Happy file opening!