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.
Example
less
Displays file content one page at a time. Allows for navigation and searching within the file.
Example
more
Displays file content one page at a time, similar to less
, but with fewer features.
Example
head
Displays the first few lines of a file (default is 10 lines).
Example
head -n <number> <file>
Displays the first <number>
lines of a file.
Example
tail
Displays the last few lines of a file (default is 10 lines).
Example
tail -n <number> <file>
Displays the last <number>
lines of a file.
Example
grep
Searches for a specific pattern within a file.
Example
find
Searches for files and directories based on criteria.
Example
File Editing Commands
nano
A simple, user-friendly text editor for the command line.
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.
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.
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.
Example
awk
A programming language for pattern scanning and processing.
Example
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.