DEV Community

Suraj Vatsya
Suraj Vatsya

Posted on

ways to open file in linux

In Linux, there are multiple ways to open a file depending on the type of file (text, image, video, etc.) and the environment you're using (command line or GUI). Here's a breakdown by method and file type:


πŸ“ From the Command Line (Terminal)

πŸ“ Text Files

  • cat filename – Displays the file content in the terminal.
  • less filename or more filename – Opens the file in a paginated viewer.
  • nano filename – Opens in the nano text editor.
  • vi filename or vim filename – Opens in vi or Vim.
  • gedit filename – Opens in the GNOME graphical editor (if installed).
  • code filename – Opens in VS Code (if installed).

πŸ“· Image Files

  • eog filename – Eye of GNOME image viewer.
  • feh filename – Lightweight terminal-based image viewer.
  • display filename – From ImageMagick.
  • xdg-open filename – Opens with default associated application.

🎬 Video/Audio Files

  • vlc filename – Opens with VLC player (if installed).
  • mpv filename – Lightweight media player.
  • xdg-open filename – Uses the default media player.

πŸ“„ PDF Files

  • evince filename
  • okular filename
  • xdg-open filename

πŸ”§ Generic Open Command

  • xdg-open filename – Opens any file with the default app for that file type.
  • open filename – macOS equivalent, works on some Linux setups if aliased.

πŸ–ΌοΈ From the GUI (Graphical User Interface)

You can:

  • Double-click the file in a file manager (Nautilus, Dolphin, Thunar, etc.)
  • Right-click β†’ "Open With" to choose an application.
  • Drag and drop the file into an open application window.

πŸ§ͺ Bonus: Scripting (in bash)

#!/bin/bash
file="example.txt"
if [ -f "$file" ]; then
    xdg-open "$file"
else
    echo "File not found!"
fi
Enter fullscreen mode Exit fullscreen mode

Top comments (0)