How To Open A File In Linux Command Line

How To Open A File In Linux Command Line

Reading time1 min
#Linux#CommandLine#OpenFiles#CLI#LinuxCommands

Mastering File Access: Command Line Techniques to Open Files on Linux Like a Pro

Forget the GUI — unlock the power of the Linux command line by mastering versatile ways to open files, from quick viewing to deep editing, all in a few keystrokes.


Navigating the Linux command line efficiently is crucial for IT professionals who want to manage files without relying on GUI tools. Mastery of file opening commands accelerates workflows and deepens system understanding. Today, we’ll break down practical, real-world ways to open files directly from the terminal — whether you need just a quick peek or full-on editing.


Why Open Files from the Command Line?

  • Speed: The command line is faster once you get used to it.
  • Remote Access: When working on servers, you won’t usually have a GUI.
  • Scriptability: Commands can be chained or scripted for automation.
  • Deeper System Control: Tools reveal file contents in ways GUIs sometimes obscure.

Quick Ways to View Files Without Editing

Sometimes you just want to take a glance or search through contents quickly…

1. cat — Concatenate and display

cat filename.txt

Displays the whole file at once. Handy for short files.

2. head / tail — Preview parts of files

head filename.txt    # First 10 lines (default)
tail filename.txt    # Last 10 lines (default)

head -n 20 filename.txt    # First 20 lines
tail -n 50 filename.txt    # Last 50 lines

These are perfect when you only need context at start or end.

3. less — Scrollable viewing with search

less filename.txt

Allows scrolling up/down freely.
Useful shortcuts inside less:

  • /searchterm → search forward
  • ?searchterm → search backward
  • q → quit less

4. strings — Extract readable text from binary files

strings /bin/ls | less

Helpful if you want to peek inside compiled binaries or strange files.


Opening Files for Editing

To truly “open” means you want to modify/save content too. Here's how:

1. nano — Beginner-friendly CLI editor

nano filename.txt
  • Simple and intuitive controls shown at bottom.
  • Ctrl+O to save, Ctrl+X to exit.

Great if you’re new and want edits without complexity.

2. vim / vi — Powerful and ubiquitous editor

vim filename.txt

Mastering Vim takes time but supercharges your productivity:

Basic usage:

  • Open file: vim filename.txt
  • Switch to insert mode: press i
  • Edit text as needed
  • Exit insert mode: press Esc
  • Save file: type :w + Enter
  • Save and exit: type :wq + Enter or simply ZZ
  • Exit without saving: type :q!

If unfamiliar, consider this step-by-step Vim tutorial after you're comfortable opening files with it.

3. Other editors you might have installed:

  • emacs filename.txt
  • micro filename.txt (a modern easy CLI editor)

Opening Files with Associated Applications (Graphical Programs) from Terminal

If your terminal is within a desktop environment and you want to open files in their default apps via command line:

On Ubuntu/Debian systems:

xdg-open filename.pdf

It will launch the default viewer associated with that filetype (e.g., PDF viewer).

Other distros might use gio open filename, which works similarly.


Bonus Tips for Power Users

Open multiple files at once in an editor:

vim file1.txt file2.log script.sh

Switch between buffers inside Vim (:bnext, :bprev) or tabs (:tabn, :tabp).


Summary Cheat-sheet Table

TaskCommandDescription
Display full contentcat filenameShow entire file
Show first N lineshead -n N filenamePreview start of file
Show last N linestail -n N filenamePreview end of file
Scroll through contentsless filenameScrollable view & searches
Simple editnano filenameBeginner-friendly editing
Advanced editvim filenamePowerful modal editing
Open in default GUI appxdg-open filenameLaunch GUI application

Wrapping Up

Mastering how to open files effectively on the Linux command line will not only speed up your workflow but also empower you with greater control over your environment — especially when working remotely or scripting management tasks. Start experimenting with these commands today, pair them with navigation (cd, ls) and you’ll graduate from casual user to confident Linux power user in no time.

Got your own favorite tips or troubles opening certain file types? Drop a comment below!


Happy Linuxing! 🚀