How To Open A File In Linux Command Line – Open Files Using Nano Or Vim

12. The Linux command line offers several ways to view a file, with `less` being ideal for scrolling through longer documents. But if you are new to Linux, you might wonder how to open a file in linux command line without a graphical interface. This guide covers all the essential commands and tools you need.

Linux gives you many choices. Some commands show the whole file at once. Others let you scroll or edit. You can even open files with specific applications from the terminal.

Let us start with the basics. Then we move to more advanced methods. By the end, you will know exactly which command to use for any situation.

How To Open A File In Linux Command Line

Opening a file in the terminal means viewing or editing its contents. Unlike Windows, Linux does not double-click. You type commands. Each command has a specific purpose.

Here are the most common ways:

  • cat – shows the entire file at once
  • less – lets you scroll through long files
  • more – similar to less but older
  • head – shows the first few lines
  • tail – shows the last few lines
  • nano – a simple text editor
  • vim – a powerful text editor

Choose based on what you need. For quick viewing, use cat or less. For editing, use nano or vim.

Using Cat To View A File

The cat command is the simplest. It prints the file content directly to the terminal.

cat filename.txt

This works well for small files. If the file is long, the text scrolls off the screen. You cannot scroll back.

Cat is great for checking config files or short logs. But avoid it for large files.

Using Less For Scrolling

Less is the best tool for long files. It shows one screen at a time. You can scroll up and down.

less filename.txt

Press Space to go forward. Press b to go back. Press q to quit.

Less also supports searching. Type / followed by a word to find it. This is very usefull for log files.

Using More For Basic Paging

More is older than less. It works similarly but with fewer features.

more filename.txt

Press Space to advance. You cannot scroll backwards. Less is generally better.

Using Head And Tail

Sometimes you only need a part of the file. Head shows the beginning. Tail shows the end.

head -n 20 filename.txt

This shows the first 20 lines. Change the number as needed.

tail -n 30 filename.txt

This shows the last 30 lines. Tail is perfect for checking recent log entries.

You can also use tail -f to follow a file in real time. New lines appear as they are added.

Editing Files With Nano

Nano is a beginner-friendly editor. It is installed on most Linux systems. To open a file for editing:

nano filename.txt

If the file does not exist, nano creates it. The interface shows commands at the bottom.

Use arrow keys to move. Type your changes. Press Ctrl+O to save. Press Ctrl+X to exit.

Nano is simple and intuitive. It is great for quick edits.

Editing Files With Vim

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

vim filename.txt

Vim starts in normal mode. You cannot type directly. Press i to enter insert mode. Now you can type.

Press Esc to return to normal mode. Type :wq to save and quit. Type :q! to quit without saving.

Vim has many commands. Learn a few basics first. It is worth the effort for heavy editing.

Opening Files With Specific Applications

Sometimes you want to open a file in a GUI application from the terminal. Use the xdg-open command.

xdg-open filename.pdf

This opens the file with the default application. It works for images, PDFs, and documents.

For text files, you can specify an editor directly:

gedit filename.txt

This opens the file in the Gedit text editor. Replace gedit with your preferred editor.

Opening Multiple Files

You can open several files at once. Most commands accept multiple filenames.

less file1.txt file2.txt

In less, type :n to go to the next file. Type :p to go to the previous file.

For editing, you can open multiple files in vim:

vim file1.txt file2.txt

Use :n and :p to switch between them.

Viewing Binary Files

Text commands do not work well for binary files. Use hexdump or xxd instead.

xxd filename.bin

This shows the file in hexadecimal format. It is usefull for debugging or analyzing binary data.

Using Od For Octal Dump

The od command shows file content in octal or other formats.

od -c filename.txt

This displays characters. It is helpful for seeing hidden characters.

Checking File Type First

Before opening a file, check its type. Use the file command.

file filename

It tells you if the file is text, binary, or something else. This prevents errors.

For example, opening a binary file with cat can mess up your terminal.

Redirecting Output To A File

Sometimes you want to save the output of a command. Use redirection.

cat file1.txt > output.txt

This writes the content to a new file. Use >> to append.

You can also combine commands with pipes. For example:

less file.txt | grep "error"

This shows only lines containing “error”.

Using Grep To Search Within Files

Grep is not for opening files, but it helps find content. Use it to locate specific text.

grep "keyword" filename.txt

This prints matching lines. Combine with other commands for powerful searches.

Common Mistakes And Fixes

New users often make these errors:

  • Typing the wrong filename – use Tab for autocomplete
  • Forgetting the file path – use pwd to check your location
  • Opening a binary file with cat – use file command first
  • Not having permission – use sudo for protected files

For permission issues:

sudo less /etc/config

This opens the file with root privileges. Be carefull when editing system files.

Using Aliases For Speed

You can create shortcuts for frequent commands. Add this to your .bashrc file:

alias mylog='less /var/log/syslog'

Now typing mylog opens the log file. This saves time.

Opening Files In Tmux Or Screen

If you use tmux or screen, you can open files in split panes. This is usefull for multitasking.

In tmux, press Ctrl+b % to split vertically. Then open a file in each pane.

Recovering Unsaved Changes

If vim crashes, you can recover your work. Use the -r option.

vim -r filename.txt

This restores the last saved version. Nano does not have this feature.

Using Emacs For Advanced Editing

Emacs is another powerful editor. To open a file:

emacs filename.txt

Emacs has a steep learning curve but offers many features. It is not installed by default on all systems.

Opening Files Over SSH

When working on a remote server, you use the same commands. SSH into the server first.

ssh user@server
less file.txt

All commands work the same way. For editing, use nano or vim.

If you need a GUI, use scp to copy the file locally first.

Using Midnight Commander

Midnight Commander is a file manager for the terminal. It shows two panels. You can browse and open files easily.

mc

Press F3 to view a file. Press F4 to edit. It is intuitive for beginners.

Opening Files With Python Or Other Scripts

You can write a script to open files. For example, a Python one-liner:

python3 -c "print(open('file.txt').read())"

This prints the file content. It is usefull for automation.

Handling Large Files

For very large files, less is still good. But if the file is gigabytes, consider using split to break it.

split -l 1000 largefile.txt part

This creates smaller files. Then open each part with less.

Using Zless For Compressed Files

If your file is compressed with gzip, use zless.

zless file.txt.gz

This decompresses and shows the content. No need to unzip first.

Viewing Man Pages

Man pages are documentation for commands. Open them with:

man less

This shows the manual for less. Use the same navigation keys.

FAQ

What is the easiest way to open a file in Linux command line?

The easiest way is using the cat command for small files or less for larger ones. Both are simple and require no setup.

How do I open a file for editing in the terminal?

Use nano filename.txt for a beginner-friendly editor. For more power, use vim filename.txt. Both let you edit and save changes.

Can I open a file in Linux command line without typing the full path?

Yes, if you are in the same directory. Use cd to navigate first. Or use relative paths like ../folder/file.txt.

What command shows the last 10 lines of a file?

Use tail -n 10 filename.txt. For real-time updates, use tail -f filename.txt.

How do I open a binary file in Linux command line?

Use xxd filename.bin or hexdump filename.bin to view it in hexadecimal. Avoid cat for binary files.

Now you know multiple ways to open files in the Linux command line. Start with cat and less. Move to nano or vim for editing. Practice each command to build confidence. The terminal becomes powerful once you master these basics.