How To Open A File In Linux Command Line

8 min read

How to Open a File in Linux Command Line: A Complete Practical Guide

Opening a file in the Linux command line is one of those fundamental skills that quietly unlocks everything else. Think about it: whether you want to peek at a configuration file, read a log, edit a script, or launch a PDF with your desktop's default application, the terminal gives you faster and more precise control than clicking through a file manager ever will. This guide walks through every practical method for opening files in Linux terminal, from the simplest read-only commands to full-screen editors, plus the troubleshooting steps that solve the errors beginners hit most often.

Quick note before moving on Most people skip this — try not to..

What "Opening a File" Actually Means in Linux

Before memorizing commands, it helps to understand what happens under the hood. In Linux, everything is a file — text documents, directories, device drivers, even network sockets. When you "open" a file, the operating system creates a file descriptor, a numeric handle that programs use to read from or write to that file.

This distinction matters because the terminal gives you two very different kinds of "opening":

  • Reading the contents into your terminal window, using commands like cat or less.
  • Launching an application that opens the file the way a graphical program would, using commands like xdg-open.

Knowing which one you need saves a lot of confusion. If you just want to see a file, you read it. If you want to work with it — edit a spreadsheet, view a photo, annotate a PDF — you launch an application.

Opening Text Files in the Terminal

cat — The Quick Peek

The most common starting point is cat, short for concatenate. It dumps the entire file to your screen:

cat notes.txt

It is perfect for short files. You can also number the output lines, which is handy when discussing code:

cat -n config.ini

The limitation is obvious: on a long file, the content scrolls past in a blur and you land at the end. That is where pagers come in.

less and more — Reading Long Files Comfortably

less is the tool most experienced Linux users reach for. It opens the file in a scrollable view without loading everything at once:

less /var/log/syslog

Inside less, these keys do most of the work:

  • Space or Page Down — move forward one screen
  • b — move backward one screen
  • / followed by text — search forward
  • n — jump to the next search match
  • G — go to the end of the file
  • g — go to the beginning
  • q — quit

more is the older, simpler cousin. It only scrolls forward and has fewer features, but you may still encounter it on minimal systems.

head and tail — The Top and Bottom

When a file has thousands of lines and you only need a sample, head and tail are faster than any pager:

head -n 20 access.log     # first 20 lines
tail -n 50 access.log     # last 50 lines

tail has one feature that makes it indispensable for administrators — following a file live:

tail -f /var/log/nginx/error.log

As new lines are written to the log, they appear on your screen in real time. Day to day, press Ctrl + C to stop. This is how you watch an application misbehave while you reproduce the problem Turns out it matters..

nl and bat — Readable Output

nl numbers lines in a stream, useful when piping output. bat is a modern, third-party alternative to cat that adds syntax highlighting and line numbers. It is not installed by default on most distributions, but if it is available, it makes reading source code in the terminal genuinely pleasant:

bat script.py

Opening Files in a Terminal Editor

Reading is passive. When you need to change something, you open the file in an editor that runs inside the terminal The details matter here..

nano — The Beginner-Friendly Choice

nano is installed nearly everywhere and works with intuitive, on-screen prompts:

nano config.ini

The shortcut bar at the bottom tells you everything: Ctrl + O to save, Ctrl + X to exit, Ctrl + K to cut a line, Ctrl + W to search. If you are new to the command line, learn nano first.

vim and vi — The Power Tool

vim has a famously steep learning curve and a correspondingly high ceiling. Open a file with:

vim config.ini

The critical thing to understand is that vim is modal. Practically speaking, in normal mode your keystrokes are commands, not text. Press i to enter insert mode and start typing. Press Esc to return to normal mode That alone is useful..

  • :w — write (save)
  • :q — quit
  • :wq or :x — save and quit
  • :q! — quit without saving

That last one is worth committing to memory. Nearly every new vim user has at some point typed :wq into a file they never meant to touch.

emacs — The Extensible Environment

emacs is less an editor than a programmable workspace. Open a file with emacs notes.Because of that, txt, or from inside emacs use Ctrl + x, Ctrl + f. Saving is Ctrl + x, Ctrl + s; quitting is Ctrl + x, Ctrl + c.

Opening Files with Their Default Application

Sometimes you want the file opened the way your desktop would open it — a PDF in a document viewer, an image in an image viewer, a spreadsheet in LibreOffice. The command for that is:

xdg-open report.pdf

xdg-open consults your desktop environment's MIME-type associations and launches the correct program. Related tools include gio open report.pdf on GNOME-based systems and open on macOS.

To find out which application is currently associated with a file type, or to change it:

xdg-mime query default application/pdf

This is also the right approach when a command-line text editor would mangle a binary format. Never open a spreadsheet or a Word document in nano expecting readable output Still holds up..

Inspecting Files You Cannot Read as Text

Not every file is text. For binaries, images, and compressed data, use inspection tools instead:

file archive.zip          # identify the file type
less report.pdf           # less can display PDFs as plain text on many systems
xxd firmware.bin | head   # hex dump
strings library.so        # extract readable strings from a binary
unzip -l archive.zip      # list archive contents without extracting
tar -tzf backup.tar.gz    # same for a tarball

file deserves special mention. It reads a file's magic bytes rather than trusting the extension, so it tells you what

what it actually is — a JPEG renamed to .Here's the thing — pdf, a gzipped tarball masquerading as . tar, or a Windows executable hiding behind no extension at all.

file mystery.dat
# mystery.dat: ASCII text, with very long lines
file image.png
# image.png: PNG image data, 1920 x 1080, 8-bit/color RGB, non-interlaced

For deeper binary analysis, xxd and hexdump -C show raw bytes with ASCII representation side by side. objdump -x binary and readelf -h binary dissect ELF executables. mediainfo video.mkv and exiftool photo.That's why jpg extract metadata from media files. Each tool answers a different question: what is this, what does it contain, how was it made.

This changes depending on context. Keep that in mind Simple, but easy to overlook..

Editing Without an Editor

Sometimes the fastest edit is no editor at all. sed and awk transform files in place or in pipelines:

sed -i 's/old_value/new_value/g' config.ini      # in-place substitution
awk -F: '$3 >= 1000 {print $1}' /etc/passwd      # list regular users

tr translates or deletes characters. But cut, paste, join, and column restructure tabular data. yq does the same for YAML. jq manipulates JSON with surgical precision. sort -u deduplicates lines. These tools compose: the output of one becomes the input of the next, letting you process gigabytes of logs without ever opening a file interactively It's one of those things that adds up..

Permissions and Ownership: The Gatekeepers

You can have the right tool and the right path, but if the permissions say no, the file stays closed.

ls -l script.sh
# -rw-r--r-- 1 alice users 4096 Jan 15 09:30 script.sh

The first column encodes three sets of three bits: owner, group, others. Plus, r (read), w (write), x (execute). Directories need x to be entered, not just r to be listed.

Change permissions with chmod:

chmod +x script.sh              # make executable
chmod 640 secrets.env           # owner rw, group r, others ---
chmod u+rw,g+r,o-rwx data.csv   # symbolic form

Change ownership with chown (requires root):

sudo chown alice:developers shared/
sudo chown -R www-data:www-data /var/www/html

Access Control Lists (ACLs) extend the model when the three-class system isn't enough:

setfacl -m u:bob:rw project/notes.md
getfacl project/notes.md

The Clipboard Bridge

Moving text between the terminal and the GUI clipboard used to be a pain. Modern tools make it trivial:

cat ssh_key.pub | xclip -selection clipboard   # X11
cat ssh_key.pub | wl-copy                      # Wayland
cat ssh_key.pub | pbcopy                       # macOS

Reverse direction:

xclip -o -selection clipboard > pasted.txt
wl-paste > pasted.txt
pbpaste > pasted.txt

Pipe anything — command output, file contents, generated passwords — straight into your browser, email, or chat window.

Remote Files: Editing Over SSH

You don't need to copy a file locally to edit it. sshfs mounts a remote filesystem locally:

mkdir ~/remote-server
sshfs user@server:/etc/nginx ~/remote-server
nano ~/remote-server/nginx.conf
fusermount -u ~/remote-server

Editors with native remote support skip the mount step. Day to day, vS Code's Remote-SSH extension, vim's scp:// and rsync:// netrw protocols, emacs's TRAMP mode — all let you open, edit, and save remote files as if they were local. The editor handles the round-trips transparently It's one of those things that adds up..

Conclusion

Opening a file is never just opening a file. Consider this: it is a decision tree: text or binary? Local or remote? Quick peek or deep edit? One-off change or scripted transformation? The command line offers a tool for every branch — cat, less, nano, vim, xdg-open, file, sed, jq, sshfs — and the glue to chain them together. Also, master the fundamentals (ls, file, less, permissions), pick one modal editor and learn its grammar, and let the specialized tools handle the rest. The file is open. Now do something useful with it That's the part that actually makes a difference..

Fresh from the Desk

Just Wrapped Up

Readers Went Here

Also Worth Your Time

Thank you for reading about How To Open A File In Linux Command Line. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home