Page cover

Viewing and Editing Files

Viewing and editing files in Linux is a crucial skill for managing configurations, monitoring logs, and troubleshooting issues. Linux provides several commands and text editors for file viewing and editing, each with unique features suited for different purposes. Here’s an expanded overview of these tools:


1. cat filename – Display the Entire File

The cat (concatenate) command is a straightforward way to display the entire content of a file. It outputs the file content directly to the terminal, making it quick and useful for viewing small files or files with short content.

  • Basic Usage:

    $ cat example.txt

    This command displays the contents of example.txt on the screen. cat is typically used for single files but can also concatenate multiple files into one, hence the name.

  • Concatenating Files:

    $ cat file1.txt file2.txt > combined.txt

    Here, cat merges file1.txt and file2.txt into a new file, combined.txt. This can be useful for combining log files or text documents.

However, cat isn’t ideal for viewing large files, as it will display the entire content in one go, which can be overwhelming and hard to scroll through.


2. nano filename – Edit Files with the Nano Text Editor

nano is a simple, user-friendly text editor accessible directly from the terminal. It’s beginner-friendly, displaying basic shortcuts at the bottom of the screen, making it an excellent choice for quickly editing files.

  • Open a File:

    $ nano example.txt

    This command opens example.txt in the Nano editor. From here, you can make changes to the file and save them easily.

  • Basic Shortcuts:

    • Save: Ctrl + O (then press Enter to confirm)

    • Exit: Ctrl + X

    • Cut Line: Ctrl + K

    • Paste Line: Ctrl + U

Nano is useful for configuration files, notes, or any text file that requires quick edits. Unlike more complex editors like vim, Nano has a minimal learning curve and allows users to save changes and exit quickly.


3. less filename – Scroll Through File Content

The less command is a viewer that displays the content of a file one page at a time, making it ideal for large files. Unlike cat, it doesn’t load the entire file into memory, so it performs well even with huge files like logs.

  • Basic Usage:

    $ less largefile.txt

    This command opens largefile.txt in a scrollable view. You can use the arrow keys to navigate up and down, making it easy to read the file without overwhelming the terminal.

  • Useful Shortcuts:

    • Scroll Down: Arrow Down or Space

    • Scroll Up: Arrow Up or b

    • Quit: q

    • Search: Type / followed by the search term, then press Enter.

less is particularly useful for log files and other large documents where you only need to view a portion of the content. It allows you to search within the file and find specific entries quickly.


4. head -n 10 filename – Show the First Few Lines of a File

The head command displays the first few lines of a file. By default, it shows the first 10 lines, but you can specify a different number with the -n option.

  • Basic Usage:

    $ head example.txt

    This command shows the first 10 lines of example.txt.

  • Specify Number of Lines:

    $ head -n 20 example.txt

    Here, head displays the first 20 lines of the file. This is useful for checking configuration files or quickly reviewing the beginning of a document without loading the entire file.

head is particularly helpful when examining structured data files (like CSVs or logs) where you may want to check headers or initial entries.


5. tail -n 10 filename – Show the Last Few Lines of a File

The tail command displays the last few lines of a file, with a default of 10 lines. This is especially useful for monitoring logs, as recent entries in logs are typically appended to the end of the file.

  • Basic Usage:

    $ tail example.txt

    This command shows the last 10 lines of example.txt.

  • Specify Number of Lines:

    $ tail -n 20 example.txt

    Here, tail displays the last 20 lines, which can help when looking for recent entries in logs.

  • Real-Time Monitoring with -f:

    $ tail -f example.log

    The -f (follow) option continuously displays new lines added to the file in real time, making it ideal for monitoring log files as they’re updated. This is useful for tracking real-time events in a system, such as error messages in application logs.

With tail -f, you can see live updates in the file, allowing you to troubleshoot issues as they occur. To stop monitoring, press Ctrl + C.


These commands provide a versatile toolkit for viewing and editing files in Linux.

  • cat: Best for quickly viewing small files or concatenating multiple files.

  • nano: Ideal for editing files, especially configuration files, thanks to its user-friendly interface.

  • less: Best for reading large files without loading the entire file into memory, with easy scrolling and search features.

  • head: Useful for checking the beginning of files or inspecting file headers.

  • tail: Ideal for viewing recent entries in logs, with -f enabling real-time monitoring.

Last updated

Was this helpful?