How To Open Files In Linux Terminal – Editing Files With Nano Editor

Using the Linux terminal to open files means choosing between text editors, file viewers, and application launchers. If you’re new to Linux, understanding how to open files in Linux terminal is a fundamental skill that will save you time and give you more control. This guide walks you through every common method, from simple text viewing to launching graphical apps from the command line.

You don’t need to be a command-line wizard. With a few basic commands, you can open, edit, and view files right from your terminal. Let’s start with the quickest ways to see what’s inside a file without changing anything.

Quick Ways To View Files Without Editing

Sometimes you just want to peek at a file’s content. The terminal offers several commands for this, each with a different purpose. The cat command is the simplest—it prints the entire file to your screen. For short files like configuration notes or simple scripts, cat works perfectly.

For longer files, less is your best friend. It shows one screenful at a time and lets you scroll up and down. Press q to quit. The head and tail commands show only the beginning or end of a file, which is great for logs or CSV headers.

  • cat filename – Print entire file to terminal
  • less filename – Scrollable viewer (press q to exit)
  • head -n 20 filename – Show first 20 lines
  • tail -n 30 filename – Show last 30 lines
  • nl filename – Show file with line numbers

These commands are read-only. They won’t modify your file. If you need to edit, you’ll need a text editor. But for quick checks, these are unbeatable.

How To Open Files In Linux Terminal With Text Editors

When you need to actually edit a file, text editors are the way to go. Linux comes with several built-in editors. The most common ones are Nano, Vim, and Emacs. Each has a different learning curve. For beginners, Nano is the easiest because it shows commands at the bottom of the screen.

Opening Files With Nano

Nano is simple and intuitive. To open a file, type nano filename. If the file doesn’t exist, Nano creates it. You’ll see the file content in the middle and a menu of commands at the bottom. The caret (^) means the Ctrl key. So ^X means press Ctrl+X to exit.

  1. Open terminal and navigate to your file’s directory using cd
  2. Type nano myfile.txt and press Enter
  3. Edit the file using your keyboard
  4. Press Ctrl+O to save, then Enter to confirm
  5. Press Ctrl+X to exit

Nano is perfect for quick edits. It doesn’t have modes like Vim, so you can start typing immediately. The only downside is that it lacks advanced features like syntax highlighting for some languages, but for most tasks it’s more than enough.

Using Vim For More Power

Vim is a modal editor. It has different modes for inserting text, navigating, and executing commands. To open a file with Vim, type vim filename. You’ll start in Normal mode. Press i to enter Insert mode and start typing. Press Esc to return to Normal mode.

To save and quit in Vim, type :wq in Normal mode and press Enter. The colon starts a command. w means write (save), and q means quit. If you want to quit without saving, type :q!.

  • i – Enter Insert mode
  • Esc – Return to Normal mode
  • :w – Save file
  • :q – Quit Vim
  • :wq – Save and quit
  • :q! – Quit without saving

Vim has a steep learning curve, but it’s incredibly powerful once you get used to it. Many system administrators prefer Vim because it’s available on almost every Linux system and works over SSH without a graphical interface.

Emacs: The Extensible Editor

Emacs is another popular editor. It’s more than just a text editor—it’s a whole environment. To open a file with Emacs in the terminal, use emacs -nw filename. The -nw flag tells Emacs to run in the terminal instead of opening a graphical window.

Emacs uses key combinations extensively. For example, Ctrl+X followed by Ctrl+S saves the file. Ctrl+X then Ctrl+C exits. Emacs has built-in tutorials and supports plugins for almost anything. It’s overkill for simple edits but excellent for coding and writing.

Opening Graphical Applications From The Terminal

You can also open graphical applications from the terminal. This is useful when you want to open a file with a specific program without clicking through menus. The xdg-open command opens a file with its default application. For example, xdg-open image.jpg opens the image in your default image viewer.

If you know the application name, you can launch it directly. For example, firefox mypage.html opens the HTML file in Firefox. gedit mynotes.txt opens the file in the Gedit text editor. This works for any application installed on your system.

  • xdg-open filename – Open with default application
  • firefox filename.html – Open in Firefox
  • gedit filename.txt – Open in Gedit
  • libreoffice filename.odt – Open in LibreOffice
  • evince filename.pdf – Open PDF in Evince

This method is great for quickly opening files while staying in the terminal. The application opens in its own window, and you can continue using the terminal for other tasks.

How To Open Files In Linux Terminal With Specialized Commands

Some file types have dedicated terminal commands. For example, od (octal dump) shows file content in different formats like hexadecimal or octal. strings extracts readable text from binary files. file tells you what type of file you’re dealing with.

For compressed files, you can view content without extracting. zcat shows the content of a gzipped file. bzcat works for bzip2 files. xzcat for xz files. These are handy when you have log files that are compressed to save space.

  1. Use file myfile to identify the file type
  2. For compressed logs: zcat syslog.gz | less
  3. For binary files: strings mybinary | grep "keyword"
  4. For hex dump: od -A x -t x1z myfile

These specialized commands give you deep insight into file contents without needing a full editor. They’re part of the standard Linux toolset and work on any distribution.

Handling Different File Types

Not all files are plain text. You might need to open PDFs, images, or office documents. In the terminal, you can use command-line tools for these too. For PDFs, pdftotext extracts text. For images, display (from ImageMagick) opens them in a window.

For office documents, libreoffice --headless --convert-to txt filename.odt converts the file to plain text. Then you can view it with cat or less. This is useful for scripting or when you don’t have a graphical environment.

  • pdftotext file.pdf – Extract text from PDF
  • display image.png – Open image in viewer
  • libreoffice –headless –convert-to txt file.docx – Convert Word to text
  • ffplay video.mp4 – Play video in terminal (requires ffmpeg)

These tools might not be installed by default. You can install them using your package manager. For example, on Ubuntu: sudo apt install poppler-utils imagemagick ffmpeg.

Opening Files With Root Privileges

Sometimes you need to edit system files that are owned by root. In that case, you need to use sudo before your command. For example, sudo nano /etc/hosts opens the hosts file with root permissions. Be careful—editing system files can break your system if you make mistakes.

Always make a backup before editing system files. You can copy the file with sudo cp /etc/hosts /etc/hosts.backup. Then if something goes wrong, you can restore it.

  1. Backup the file: sudo cp /etc/fstab /etc/fstab.backup
  2. Open with editor: sudo nano /etc/fstab
  3. Make your changes and save
  4. Verify the file: cat /etc/fstab

Using sudo with a text editor is the standard way to edit configuration files. Never run your regular editor as root for normal files—it’s a security risk.

Common Mistakes And How To Avoid Them

Beginners often run into a few common issues. One is trying to open a binary file with a text editor. This will show garbled characters and might crash the editor. Always check the file type first with file.

Another mistake is forgetting to use sudo for system files. You’ll get a “Permission denied” error. Just add sudo before your command. Also, remember that cat is not for large files—use less instead to avoid flooding your terminal.

  • Always check file type before opening
  • Use sudo for system files
  • Use less for large files
  • Make backups before editing important files
  • Know your editor’s exit command (Ctrl+X for Nano, :q for Vim)

If you get stuck in Vim, press Esc a few times, then type :q! and Enter. This will quit without saving. If you’re in Nano and can’t exit, press Ctrl+X and follow the prompts.

Automating File Opening With Aliases

You can create shortcuts for commands you use often. Aliases are stored in your shell configuration file (usually ~/.bashrc or ~/.zshrc). For example, you can create an alias to open a file with your preferred editor:

alias edit='nano'

Then you can type edit myfile.txt instead of nano myfile.txt. You can also create aliases for opening specific files or directories:

  • alias hosts='sudo nano /etc/hosts'
  • alias logs='cd /var/log && ls -lh'
  • alias config='nano ~/.bashrc'

After adding aliases, reload your configuration with source ~/.bashrc. This saves time and reduces typing errors.

Using Wildcards To Open Multiple Files

You can open multiple files at once using wildcards. For example, nano *.txt opens all text files in the current directory. Nano will show them one at a time. You can switch between files using Ctrl+X and then selecting the next file.

For viewing, cat *.log concatenates all log files and prints them. This is useful for combining logs. But be careful—if there are many files, the output can be huge. Use cat *.log | less to scroll through.

  1. Open all .conf files: nano /etc/*.conf
  2. View all .txt files: less *.txt
  3. Combine all .csv files: cat *.csv > combined.csv

Wildcards are powerful but can be dangerous. Always double-check which files match before running a command that modifies them.

How To Open Files In Linux Terminal Remotely

If you’re connected to a remote server via SSH, you can open files just like on your local machine. The same commands work. However, graphical applications won’t open unless you use X11 forwarding. Add the -X flag to your SSH command: ssh -X user@server.

For editing remote files, you can also use scp to copy the file to your local machine, edit it, and copy it back. Or use rsync for syncing directories. But for quick edits, just use Nano or Vim over SSH.

  • ssh -X user@server – Enable X11 forwarding
  • scp user@server:/path/to/file . – Copy file to local machine
  • nano /path/to/file – Edit directly over SSH

Remote editing is a common task for sysadmins. Learning to use terminal editors efficiently will make you much faster when working on servers.

FAQ: How To Open Files In Linux Terminal

What Is The Easiest Way To Open A Text File In Linux Terminal?

The easiest way is using nano filename. Nano is simple, shows commands at the bottom, and works on almost every Linux system. If you just want to view the file without editing, use less filename.

How Do I Open A PDF File In Linux Terminal?

You can use xdg-open file.pdf to open it with the default PDF viewer. For text extraction, use pdftotext file.pdf and then view the output with less or cat.

Can I Open A File With A Specific Application From The Terminal?

Yes, just type the application name followed by the filename. For example, firefox index.html or gedit notes.txt. If the application isn’t in your PATH, provide the full path.

How Do I Open A File As Root In Linux Terminal?

Use sudo before your command. For example, sudo nano /etc/hosts. Always be careful when editing system files—make a backup first.

What Should I Do If I Get “Permission Denied” When Opening A File?

This means you don’t have read permission for that file. Use ls -l filename to check permissions. If it’s owned by root, use sudo. If it’s owned by another user, you may need to change permissions with chmod or ask the owner.

Final Tips For Mastering File Opening In Terminal

Practice these commands daily. Start with cat and less for viewing, then move to Nano for editing. Once you’re comfortable, try Vim for more advanced editing. Remember that the terminal is just another tool—it’s not scary once you know a few commands.

Keep a cheat sheet handy. Write down the commands you use most often. Over time, they’ll become second nature. And don’t be afraid to experiment—you can’t break anything by just viewing files. Editing requires caution, but with backups, you’re safe.

Now you know how to open files in Linux terminal using multiple methods. Whether you’re viewing logs, editing configs, or launching graphical apps, the terminal gives you speed and flexibility. Start with the basics, and you’ll be a command-line pro in no time.