How to Open a File in Linux: Commands, Tools, and When to Use Each

Linux gives you more ways to open a file than most operating systems — and that's both its strength and its initial learning curve. Whether you're working at the command line or inside a desktop environment, the method you use matters. The right approach depends on what type of file it is, what you want to do with it, and how your system is set up.

What "Opening a File" Actually Means in Linux

On Windows or macOS, opening a file usually means double-clicking it and letting the OS figure out the rest. Linux works the same way on the desktop — but it also exposes the layer underneath, where every file interaction is a deliberate command.

"Opening" a file in Linux can mean several different things:

  • Viewing the contents (reading a log, checking a config file)
  • Editing the contents (modifying a script, updating a settings file)
  • Executing it (running a program or shell script)
  • Launching it with a specific application (opening an image in a viewer, a PDF in a reader)

The method changes depending on which of these you actually need.

Opening Files from the Terminal

The terminal is where Linux power lives. Here are the most common approaches:

Viewing File Contents

cat is the simplest option — it prints the entire file to the terminal at once:

cat filename.txt 

Good for short files. For anything longer, the output scrolls past faster than you can read it.

less is the better choice for longer files. It opens an interactive, scrollable view:

less filename.txt 

Navigate with the arrow keys, Page Up/Down, and press q to quit.

head and tail let you see just the beginning or end of a file — useful for logs:

head -n 20 filename.txt tail -n 50 logfile.log 

tail -f is especially handy for watching a log file update in real time.

Editing Files from the Terminal

This is where text editors come in. The three most common terminal-based editors are:

EditorSkill LevelBest For
nanoBeginner-friendlyQuick edits, config files
vim / viIntermediate–AdvancedPower users, scripting
emacsAdvancedDevelopers with specific workflows

To open a file with nano:

nano filename.txt 

To open with vim:

vim filename.txt 

Nano shows keyboard shortcuts at the bottom of the screen. Vim requires knowing its modal interface — pressing i to enter insert mode, Esc to return to normal mode, and :wq to save and quit. There's a reason "how to exit Vim" is one of the most searched programming questions on the internet. 😄

Opening Files with a Specific Application

The xdg-open command tells Linux to open a file using whatever application is associated with that file type in your desktop environment:

xdg-open document.pdf xdg-open image.png 

This is the terminal equivalent of double-clicking. It works when you're running a desktop environment (GNOME, KDE, XFCE, etc.) and may not function on headless servers with no graphical interface.

You can also call a specific application directly:

libreoffice document.docx gimp photo.png vlc video.mp4 

Opening Files in a Desktop Environment

If you're running a Linux distribution with a GUI — Ubuntu, Fedora, Linux Mint, and others — opening files works much like it does on other operating systems:

  • File manager (Nautilus on GNOME, Dolphin on KDE, Thunar on XFCE): browse and double-click
  • Right-click → Open With: choose a specific application
  • Application menus: open apps first, then use File → Open

The file manager reads MIME types to determine which application opens which file. If a file type isn't associated with an app, the system will prompt you to choose one.

Permissions Matter 🔑

One thing Linux enforces strictly: file permissions. If you don't have read access to a file, no amount of clever commands will open it — you'll get a "Permission denied" error.

You can check permissions with:

ls -l filename.txt 

The output shows who owns the file and what read/write/execute rights apply to the owner, group, and others. If you need to open a system file or a file owned by root, you'll typically prefix your command with sudo:

sudo nano /etc/hosts 

Use sudo carefully. It elevates your privileges to root level, and editing the wrong system file can break things.

Executing a File vs. Opening It

There's an important distinction Linux makes that other systems blur: running a file is not the same as opening it.

A shell script (.sh) or binary file can be opened in a text editor to view its contents, or executed as a program. To execute a script, it needs the execute permission set, and you run it with:

./script.sh 

Without the ./, Linux won't look in the current directory. Without execute permission, it won't run at all. You can grant execute permission with:

chmod +x script.sh 

Factors That Shape Which Method Works for You

The "right" way to open a file in Linux shifts based on several variables:

  • Whether you have a GUI or are on a headless server — terminal-only environments eliminate desktop tools entirely
  • The file type — plain text, binary, image, PDF, and executable files each have different appropriate tools
  • Your comfort with the command line — nano versus vim is itself a meaningful skill-level distinction
  • Your Linux distribution — default applications and file managers vary across distros
  • Your permissions level — regular user versus sudoer changes what's accessible

A developer on a remote server via SSH will open files very differently than someone running Ubuntu as a daily desktop OS. Both approaches are valid — but the tools, commands, and mental model needed are genuinely different. 🖥️

Understanding which of those situations you're in is the starting point for figuring out which method actually fits your workflow.