首页 > Sysadmin > 正文

8 Ways to Open Files in Linux: cat, less, vim, xdg-open and More

2026-11-02 Sysadmin

From the terminal to the graphical interface, this guide covers eight common ways to open files on Linux, matched to different scenarios. Tested on WSL2.

1. TL;DR: Pick by scenario

  • Just want to read content → cat, less, more, head, tail, bat
  • Need to edit the file → nano, vim
  • Open with the default app (images, PDFs) → xdg-open, gio
  • Desktop environment → double-click or right-click

2. Read-only: cat / less / more / head / tail

cat prints the whole file at once [1]. Fine for small files; for large ones, use less instead.

less supports paging and search [2]. It doesn’t load the whole file into memory, so it starts quickly.

more is simpler; it only pages forward [3].

head shows the first 10 lines; add -n to set the count [4].

tail shows the last 10 lines; -f follows logs in real time [5].

cat /etc/hostname
less /var/log/syslog
more /etc/passwd
head -n 5 /etc/fstab
tail -f /var/log/auth.log

3. Modern viewing: bat

bat is a Rust-based replacement for cat with syntax highlighting and Git integration [6]. It is not installed by default on Ubuntu 24.04; install it with apt install bat and call it as batcat.

bat /etc/hostname

4. Terminal editors: nano / vim

nano is simple and beginner-friendly [7]. Save with Ctrl+O, exit with Ctrl+X.

vim is the most powerful option but has a steep learning curve. After opening, press i to enter insert mode; use :wq to save and quit.

nano /etc/fstab
vim /etc/fstab

5. GUI opening: xdg-open / gio

In a desktop environment, xdg-open opens files with the system default application [8]. gio does the same thing with GIO support [9]. For example, opening a PDF uses Evince, and opening an image uses Eye of GNOME.

xdg-open document.pdf
gio open image.png

WSL without a desktop environment will fail on both commands. You need WSLg or a Windows-side default-program mapping.

6. Troubleshooting cheat sheet

  • cat: No such file or directory: wrong path — run ls to confirm.
  • less: No such file or directory: wrong path — run ls to confirm.
  • vim: Not an editor command: vim is not installed — run apt install vim.
  • xdg-open: command not found: xdg-utils is missing — run apt install xdg-utils.
  • gio: Failed to find default application: no desktop environment or MIME type not registered — check $XDG_DATA_HOME/applications.

Rollback / undo:

  • Read-only commands don’t modify files.
  • Editors: nano creates a backup .~lock.<file> by default; vim can discard changes with :e!.
  • GUI-open commands don’t modify files.

Limitations:

  • xdg-open / gio need a desktop environment. WSL has no display server by default, so default-app launching cannot be verified in a headless session.
  • bat is not preinstalled and requires extra setup.
  • more has limited functionality; less is recommended.

—

Status: published


References

The commands in this article were tested on WSL Ubuntu (24.04) and cross-checked against Debian and general-distro documentation.