How To Open A Text File In Linux : Cat Or Nano Command Usage

Launching a program from the Linux terminal is often faster than clicking through graphical menus, and it gives you immediate access to its output logs. Knowing how to open a text file in Linux is a fundamental skill that every user should master, whether you’re editing configuration files, reading logs, or writing code. This guide will walk you through every major method, from simple commands to advanced editors, so you can choose the right tool for any situation.

Text files are everywhere in Linux—from system configurations in /etc to personal notes in your home directory. The terminal offers powerful ways to view, edit, and manipulate these files without ever touching a mouse. Let’s start with the quickest methods and work our way up to more feature-rich options.

How To Open A Text File In Linux

Before diving into specific commands, it helps to understand that Linux treats everything as a file. This means your text files are just streams of data that can be read, written, or piped to other programs. The method you choose depends on what you need to do: just peek at the content, make quick edits, or work on a large project.

Using Cat To View File Contents

The cat command is the simplest way to display a file’s content directly in the terminal. It’s perfect for short files like configuration snippets or log entries.

  1. Open your terminal (Ctrl+Alt+T on most distributions).
  2. Type cat filename.txt and press Enter.
  3. The entire file content prints to the screen.

For example, to view a file named notes.txt, you’d run cat notes.txt. The output appears immediately, but if the file is long, it scrolls past quickly. That’s where less comes in handy.

One common mistake is using cat on binary files—it will spew gibberish and might mess up your terminal. Stick to text files only. Also, remember that cat doesn’t let you edit; it’s strictly a viewer.

Using Less For Scrolling Through Long Files

When you need to read a large log file or a document that spans hundreds of lines, less is your best friend. It paginates the content so you can scroll up and down at your own pace.

  • Run less filename.txt to open the file.
  • Use the arrow keys or Page Up/Page Down to navigate.
  • Press q to quit and return to the terminal.
  • Search for text by typing /searchterm and pressing Enter.
  • Press n to jump to the next match.

Unlike cat, less doesn’t load the entire file into memory at once. This makes it ideal for multi-megabyte log files that would overwhelm your terminal. You can also open multiple files with less file1.txt file2.txt and switch between them using :n (next) and :p (previous).

Pro tip: If you accidentally open a binary file with less, it will show a warning. Press q immediately to exit safely.

Using More For Basic Pagination

The more command is an older cousin of less with fewer features. It still works on most Linux systems and is useful for quick glances at files.

  • Type more filename.txt to view the file page by page.
  • Press the spacebar to advance one screen.
  • Press Enter to scroll line by line.
  • Press q to quit.

One limitation of more is that you can’t scroll backward. Once you pass a section, you have to restart the command to see it again. For most modern users, less is a better choice, but more is still good to know for older systems or scripts.

Using Head And Tail For File Beginnings And Ends

Sometimes you only need to see the first few lines or the last few lines of a file. The head and tail commands are perfect for this.

  • head -n 20 filename.txt shows the first 20 lines.
  • tail -n 15 filename.txt shows the last 15 lines.
  • tail -f filename.txt follows the file in real-time, showing new lines as they’re added.

The -f option is invaluable for monitoring log files. For example, tail -f /var/log/syslog lets you watch system events as they happen. Press Ctrl+C to stop following.

You can combine head and tail with other commands using pipes. For instance, cat largefile.txt | tail -n 100 | head -n 10 shows lines 91-100 of a file. This is a quick way to extract a specific section without opening the whole thing.

Using Nano For Quick Edits

When you need to make a quick change to a configuration file or write a short note, nano is the easiest terminal-based editor. It’s installed by default on most Linux distributions and has a simple interface.

  1. Run nano filename.txt to open the file in the editor.
  2. Use the arrow keys to move the cursor.
  3. Type or delete text as needed.
  4. Press Ctrl+O to save (write out) the file.
  5. Press Ctrl+X to exit.

Nano shows helpful shortcuts at the bottom of the screen. The caret (^) symbol represents the Ctrl key, so ^O means Ctrl+O. If you make a mistake, you can undo with Ctrl+Z (in newer versions) or cut lines with Ctrl+K.

One thing to watch out for: Nano doesn’t create a backup by default. If you accidentally delete everything and save, the original content is gone. Consider using the -B flag to create a backup file: nano -B filename.txt.

Using Vim For Power Editing

Vim is a modal editor with a steep learning curve but incredible power. Once you get comfortable with its modes and commands, you can edit files faster than with any graphical editor.

  • Open a file with vim filename.txt.
  • You start in Normal mode. Press i to enter Insert mode and start typing.
  • Press Esc to return to Normal mode.
  • Type :w and press Enter to save.
  • Type :q and press Enter to quit.
  • Type :wq to save and quit together.

Vim has dozens of commands for navigation, searching, and replacing. For example, dd deletes the current line, yy copies it, and p pastes it. To search, type /searchterm in Normal mode and press Enter.

If you open a file and don’t know what to do, press Esc a few times, then type :q! to quit without saving changes. The exclamation mark forces the quit, overriding any unsaved changes.

Many beginners find Vim confusing at first. Stick with it for a week, and you’ll see why it’s a favorite among sysadmins and developers. The muscle memory pays off.

Using Emacs For A Different Approach

Emacs is another powerful editor with a completely different philosophy. Instead of modes, it uses key chords (combinations of Ctrl, Alt, and other keys) for most operations.

  • Run emacs filename.txt to open the file.
  • Start typing immediately—Emacs is always in insert mode.
  • Press Ctrl+X Ctrl+S to save.
  • Press Ctrl+X Ctrl+C to quit.

Emacs can be extended with packages for almost any task, from email clients to web browsers. For simple text editing, though, it might feel overkill. The learning curve is similar to Vim’s, but the key combinations are different.

If you’re on a server without a graphical environment, you might need to run emacs -nw to start Emacs in terminal mode. Otherwise, it may try to open a graphical window.

Using Graphical Text Editors From The Terminal

You don’t have to stay in the terminal to edit files. You can launch graphical editors like Gedit, Kate, or VS Code from the command line.

  • gedit filename.txt opens the file in GNOME’s default editor.
  • kate filename.txt opens it in KDE’s editor.
  • code filename.txt opens it in Visual Studio Code.

This approach combines the speed of terminal navigation with the comfort of a graphical interface. You can quickly find a file using cd and ls, then open it with your preferred editor.

One tip: Append an ampersand (&) at the end of the command to run the editor in the background. For example, gedit notes.txt & lets you continue using the terminal while the editor is open.

Using Redirection And Pipes For Advanced File Handling

Beyond opening files directly, you can use shell redirection to create or modify text files without an editor.

  • echo "Hello, World!" > newfile.txt creates a file with that text.
  • echo "Second line" >> newfile.txt appends text to an existing file.
  • cat file1.txt file2.txt > combined.txt merges two files.

Pipes let you send the output of one command as input to another. For example, ls -la | grep ".txt" > textfiles.txt lists all .txt files and saves the list to a file. This is a powerful way to process and save data without opening an editor.

You can also use tee to split output between the screen and a file: command | tee output.txt. This shows the output in real-time while saving it.

Using Sed And Awk For In-Place Editing

For scripted or bulk edits, sed (stream editor) and awk are invaluable. They let you modify files without opening them interactively.

  • sed -i 's/oldtext/newtext/g' filename.txt replaces all occurrences of “oldtext” with “newtext” in the file.
  • awk '{print $1}' filename.txt prints the first word of each line.
  • awk '/error/ {print}' logfile.txt prints lines containing “error”.

These commands are essential for automating repetitive tasks. For instance, you can update a configuration file across multiple servers by running a single sed command via SSH.

Be careful with sed -i—it modifies the file in place. Always test your command on a copy first, or use sed -i.bak to create a backup file with a .bak extension.

Using File Managers With Terminal Integration

Some file managers, like Midnight Commander (mc), offer a terminal-based interface for browsing and opening files. You can navigate directories, preview files, and open them in your default editor.

  • Install Midnight Commander with sudo apt install mc (Debian/Ubuntu) or sudo dnf install mc (Fedora).
  • Run mc to start the file manager.
  • Use arrow keys to select a file and press F4 to edit it.

This is a good middle ground for users who prefer visual navigation but don’t have a graphical desktop. It’s especially useful over SSH connections.

Handling Special File Types

Not all text files are plain ASCII. Linux supports various encodings and line endings. If a file looks garbled, you might need to specify the encoding.

  • Use file -i filename.txt to check the file’s encoding.
  • Open with a specific encoding in Nano: nano --encoding=utf-8 filename.txt.
  • Convert encodings with iconv -f old_encoding -t new_encoding filename.txt > newfile.txt.

Common encodings include UTF-8, ISO-8859-1, and Windows-1252. Line endings can be Unix (LF) or Windows (CRLF). Use dos2unix and unix2dos to convert between them.

Opening Files With Root Privileges

System configuration files often require root access to edit. You can use sudo with any editor.

  • sudo nano /etc/hosts opens the hosts file for editing.
  • sudo vim /etc/ssh/sshd_config opens the SSH server configuration.

Be extremely careful when editing system files. A typo can break your system. Always make a backup first: sudo cp /etc/hosts /etc/hosts.backup.

If you accidentally save a file with incorrect permissions, you can fix them with sudo chmod 644 filename.txt or sudo chown user:group filename.txt.

Opening Remote Files Over SSH

You can edit files on remote servers directly from your local terminal using SSH.

  • ssh username@server.com "cat /path/to/file.txt" displays the file remotely.
  • ssh username@server.com "nano /path/to/file.txt" opens it in Nano on the remote server.
  • Use scp to copy the file locally, edit it, then copy it back.

For a more seamless experience, consider using SSHFS to mount the remote directory locally. Then you can open files with any local editor.

Using Wildcards To Open Multiple Files

You can open multiple files at once using wildcards. For example, cat *.txt concatenates all text files in the current directory. nano file1.txt file2.txt opens both files in Nano, switching between them with Alt+Left/Right.

Be cautious with wildcards—cat * will try to display every file, including binaries, which can crash your terminal. Always narrow your pattern to text files only.

Using The Open Command (For Desktop Environments)

Some Linux distributions include an open or xdg-open command that opens a file with its default application.

  • xdg-open filename.txt opens the file in your default text editor.
  • open filename.txt works on some systems (like elementary OS).

This is the closest Linux equivalent to double-clicking a file in a graphical file manager. It’s handy when you want to use the system’s default editor without remembering its name.

Creating And Opening New Files

To create a new text file and open it immediately, you can combine commands.

  • touch newfile.txt && nano newfile.txt creates an empty file, then opens it.
  • echo "Initial content" > newfile.txt && vim newfile.txt creates a file with content, then opens it.

The touch command is useful for creating empty files quickly. If the file already exists, it updates its timestamp without changing the content.

Recovering Unsaved Changes

If your editor crashes or you close it accidentally, you might lose unsaved work. Some editors have recovery features.

  • Vim saves swap files (.filename.txt.swp) that