Skip to content

Linux File Viewing and Editing Commands

Overview

Linux provides several commands for viewing and editing files. These commands allow you to read, search, and modify file content from the command line. This document covers basic and commonly used file viewing and editing commands.

File Viewing Commands

cat

Displays the entire content of a file.

cat <file>

Example

cat file1.txt
# Output: Displays the content of 'file1.txt'

less

Displays file content one page at a time. Allows for navigation and searching within the file.

less <file>

Example

less file1.txt
# Output: Displays the content of 'file1.txt' one page at a time
# Use 'q' to quit

more

Displays file content one page at a time, similar to less, but with fewer features.

more <file>

Example

more file1.txt
# Output: Displays the content of 'file1.txt' one page at a time
# Use 'q' to quit

Displays the first few lines of a file (default is 10 lines).

head <file>

Example

head file1.txt
# Output: Displays the first 10 lines of 'file1.txt'

head -n <number> <file>

Displays the first <number> lines of a file.

head -n <number> <file>

Example

head -n 20 file1.txt
# Output: Displays the first 20 lines of 'file1.txt'

tail

Displays the last few lines of a file (default is 10 lines).

tail <file>

Example

tail file1.txt
# Output: Displays the last 10 lines of 'file1.txt'

tail -n <number> <file>

Displays the last <number> lines of a file.

tail -n <number> <file>

Example

tail -n 20 file1.txt
# Output: Displays the last 20 lines of 'file1.txt'

grep

Searches for a specific pattern within a file.

grep "<pattern>" <file>

Example

grep "error" log.txt
# Output: Displays lines containing 'error' in 'log.txt'

find

Searches for files and directories based on criteria.

find <path> -name "<filename>"

Example

find /home/user/ -name "*.txt"
# Output: Finds all '.txt' files in '/home/user/' directory

File Editing Commands

nano

A simple, user-friendly text editor for the command line.

nano <file>

Example

nano file1.txt
# Opens 'file1.txt' in the nano editor for editing
# Use 'Ctrl + X' to exit, 'Y' to confirm changes, 'N' to discard changes

vim

A powerful text editor with a steep learning curve but extensive capabilities.

vim <file>

Example

vim file1.txt
# Opens 'file1.txt' in the vim editor for editing
# Use ':wq' to save and exit, ':q!' to exit without saving

vi

An older text editor similar to vim, often available by default.

vi <file>

Example

vi file1.txt
# Opens 'file1.txt' in the vi editor for editing
# Use ':wq' to save and exit, ':q!' to exit without saving

sed

A stream editor for filtering and transforming text.

sed 's/<pattern>/<replacement>/' <file>

Example

sed 's/oldtext/newtext/' file1.txt
# Replaces 'oldtext' with 'newtext' in 'file1.txt'

awk

A programming language for pattern scanning and processing.

awk '{print $1}' <file>

Example

awk '{print $1}' file1.txt
# Prints the first column of 'file1.txt'

Summary

Linux provides a range of commands for viewing and editing files, from simple viewers like cat and less to powerful editors like vim and nano. Understanding these commands helps manage and manipulate file content effectively from the command line. For more detailed information on each command, refer to the Linux manual pages.