How To Open Txt File In Linux – Cat And Less Commands

Opening a TXT file in Linux works best with command-line tools like cat or less. If you’re wondering how to open txt file in linux, you’ve come to the right place. This guide covers every method, from simple terminal commands to graphical editors, so you can pick what fits your workflow.

Linux offers multiple ways to view and edit text files. Whether you’re a beginner or a seasoned user, these steps are easy to follow. Let’s jump right in.

How To Open Txt File In Linux

Before we get into the details, understand that Linux treats everything as a file. A TXT file is just plain text, so opening it is straightforward. You can use the terminal or a GUI app, depending on your preference.

Most Linux distributions come with pre-installed tools. You don’t need to install anything extra for basic tasks. Let’s explore the most common methods.

Using Cat Command

The cat command is the simplest way to display a text file’s content. It prints the entire file to the terminal.

  1. Open your terminal (Ctrl+Alt+T on most systems).
  2. Type: cat filename.txt
  3. Press Enter. The file content appears immediately.

Cat is great for short files. For long files, it scrolls past quickly, which isn’t ideal. Use it when you need a quick glance.

Using Less Command

The less command is better for large files. It shows one screen at a time, letting you scroll up and down.

  1. In the terminal, type: less filename.txt
  2. Use the arrow keys or Page Up/Down to navigate.
  3. Press q to exit.

Less is my go-to for reading logs or long documents. It’s efficient and doesn’t overload your terminal.

Using More Command

The more command is similar to less but older. It shows content page by page.

  1. Type: more filename.txt
  2. Press Spacebar to go to the next page.
  3. Press q to quit.

More is less flexible than less (pun intended). Stick with less if you have the choice.

Using Head And Tail Commands

Head and tail show only parts of a file. Head displays the first 10 lines by default; tail shows the last 10.

  • head filename.txt – first 10 lines
  • tail filename.txt – last 10 lines
  • head -n 20 filename.txt – first 20 lines
  • tail -n 5 filename.txt – last 5 lines

These are perfect for checking file headers or recent log entries.

Using Nano Editor

Nano is a simple text editor that runs in the terminal. It’s user-friendly for beginners.

  1. Type: nano filename.txt
  2. The file opens in an editor. You can type, delete, and save changes.
  3. Press Ctrl+O to save, then Ctrl+X to exit.

Nano shows shortcuts at the bottom, so you don’t need to memorize commands. It’s ideal for quick edits.

Using Vim Editor

Vim is powerful but has a learning curve. To open a file:

  1. Type: vim filename.txt
  2. Press i to enter insert mode and edit.
  3. Press Esc, then type :wq to save and quit.

Vim offers advanced features like syntax highlighting and macros. If you’re new, start with nano.

Using Emacs Editor

Emacs is another powerful editor. Open a file with:

  1. Type: emacs filename.txt
  2. Use Ctrl+X Ctrl+F to open a new file within Emacs.
  3. Edit as needed, then Ctrl+X Ctrl+S to save.

Emacs can be extended with plugins. It’s overkill for simple text files but great for coding.

Using Graphical Text Editors

If you prefer a GUI, Linux has many options. Common ones include:

  • Gedit (GNOME) – simple and clean
  • Kate (KDE) – feature-rich
  • Mousepad (Xfce) – lightweight
  • VS Code – for developers

To open a file with Gedit, type: gedit filename.txt in the terminal, or double-click the file in your file manager.

Using File Manager

Most file managers let you open TXT files with a double-click. The default text editor opens automatically.

  1. Navigate to your file in Nautilus, Dolphin, or Thunar.
  2. Double-click the file.
  3. It opens in the default text editor.

You can change the default app by right-clicking and selecting “Open With.”

Using Redirection And Pipes

You can combine commands to view files creatively. For example:

  • cat file.txt | less – pipe cat output to less
  • head -n 50 file.txt | tail -n 10 – show lines 41-50

This is useful for filtering content without opening the whole file.

Opening Multiple Files

You can open several files at once with most commands:

  • cat file1.txt file2.txt – concatenates and displays both
  • less file1.txt file2.txt – opens first, then type :n for next
  • vim file1.txt file2.txt – opens both in buffers

This saves time when comparing or reviewing multiple files.

Using Grep To Search Inside Files

If you need to find specific text, grep is your friend:

  • grep "search term" filename.txt – shows matching lines
  • grep -i "search" filename.txt – case-insensitive search
  • grep -n "search" filename.txt – shows line numbers

Combine with cat or less for more control: cat file.txt | grep "error"

Handling Large Files

For files over 100MB, avoid cat. Use less or split the file:

  • split -l 1000 largefile.txt smallfile – splits into 1000-line chunks
  • less largefile.txt – efficient scrolling

Large files can crash GUI editors. Stick to terminal tools for stability.

Opening Files With Special Characters

If your filename has spaces or special chars, use quotes or escape them:

  • cat "my file.txt" – quotes work
  • cat my\ file.txt – backslash escapes space

Tab completion (press Tab) helps avoid typos.

Using Sed For In-Place Editing

Sed can edit files without opening them. For example, replace “old” with “new”:

  • sed -i 's/old/new/g' filename.txt – replaces all occurrences

Be careful with sed – changes are permanent. Test with sed 's/old/new/g' filename.txt (no -i) first.

Using Awk For Structured Data

Awk is great for formatted text files. Print specific columns:

  • awk '{print $1, $3}' filename.txt – prints first and third columns

This is handy for CSV-like files.

Common Mistakes To Avoid

  • Using cat on binary files – it garbles your terminal
  • Forgetting to save in nano – use Ctrl+O
  • Typing wrong filename – double-check with ls
  • Opening huge files in GUI – freezes your system

Always verify your file exists first with ls -l filename.txt.

Choosing The Right Tool

Here’s a quick guide based on your need:

  • Quick view: cat or head/tail
  • Long file browsing: less
  • Simple edit: nano
  • Advanced editing: vim or emacs
  • GUI preference: gedit or kate
  • Searching: grep

Experiment with each to find what feels natural.

Keyboard Shortcuts For Terminal Editors

Boost your speed with these shortcuts:

  • Nano: Ctrl+W to search, Ctrl+K to cut line
  • Vim: / to search, dd to delete line, yy to copy
  • Less: / to search, n for next match, N for previous

Practice makes these second nature.

Opening Files From Remote Servers

If you’re SSH’d into a server, use the same commands. For example:

  • ssh user@server – connect
  • less /var/log/syslog – view remote file

You can also use scp to copy files locally: scp user@server:/path/to/file.txt .

Using Midnight Commander

Midnight Commander (mc) is a file manager in the terminal. It shows two panels and lets you open files with F3 (view) or F4 (edit).

  1. Install: sudo apt install mc (Debian/Ubuntu)
  2. Run: mc
  3. Navigate to your file and press F3 to view.

It’s handy for browsing directories and opening files quickly.

Automating File Opening With Aliases

Create shortcuts in your .bashrc file:

  • alias view='less' – now “view file.txt” works
  • alias edit='nano' – “edit file.txt” opens nano

Reload with source ~/.bashrc. This saves typing.

Handling Encoded Text Files

Sometimes files have different encodings (UTF-8, ISO-8859-1). Use file command to check:

  • file filename.txt – shows encoding
  • iconv -f ISO-8859-1 -t UTF-8 filename.txt – converts encoding

This prevents garbled text in editors.

Opening Files With Root Privileges

For system files, use sudo:

  • sudo less /etc/hosts – view as root
  • sudo nano /etc/hosts – edit as root

Be cautious – editing system files can break things.

Using Bat As Cat Alternative

Bat is a modern cat clone with syntax highlighting. Install it:

  • sudo apt install bat (Debian/Ubuntu)
  • Use: bat filename.txt

It shows line numbers and git modifications. A nice upgrade.

Viewing Non-Text Files

If you accidentally open a binary file, your terminal may glitch. Use reset to fix it. Always check file type with file first.

Summary Of Commands

Command Use Case
cat Quick display
less Scrolling through large files
more Page-by-page view
head/tail First/last lines
nano Simple editing
vim Advanced editing
gedit GUI editing

Bookmark this table for quick reference.

Frequently Asked Questions

How do I open a txt file in Linux terminal?

Use cat, less, or nano. For example: cat file.txt or nano file.txt.

What is the best way to view a large txt file in Linux?

Use less command. It loads only part of the file, so it’s fast and memory-efficient.

Can I open a txt file in Linux without terminal?

Yes, double-click the file in your file manager. It opens in the default text editor like Gedit or Kate.

How do I edit a txt file in Linux command line?

Use nano for simplicity: nano file.txt. Or vim for more features: vim file.txt.

Why does my txt file show garbled text in Linux?

It might be a binary file or wrong encoding. Use file filename.txt to check, then convert with iconv if needed.

Final Tips

Practice these commands daily. Start with cat and less, then move to nano. You’ll soon navigate files like a pro.

Remember, the terminal is your friend. It’s faster than GUI for most tasks. Don’t be afraid to experiment – you can’t break anything by viewing files.

If you get stuck, use man command (e.g., man less) for help. The manual pages are comprehensive.

Now you know how to open txt file in linux using multiple methods. Choose what works for you and stick with it. Happy file viewing!