College-Level Course Module | RHEL System Administration
Learning Objectives
1
View text file contents
Use cat, less, head, tail, and other viewing commands
2
Create text files
Use redirection, here documents, and editors
3
Edit files with vim
Navigate, insert, modify, search, and save in vim
4
Edit files with nano
Use nano for quick and simple text editing
Why Text Files Matter
In Linux, everything is configured with text files. System settings, service configurations, scripts, logs - all are plain text that you can read and edit.
/var/log/messages - system log
/var/log/secure - security log
/usr/local/bin/*.sh - local scripts
~/.bash_history - command history
Cron jobs, systemd units, etc.
Philosophy: Text files are human-readable, versionable, scriptable, and transparent. No proprietary formats or special tools needed.
Viewing Files: cat
cat (concatenate) displays file contents to the terminal. Best for small files - large files scroll by too quickly.
# Display file contents[user@host ~]$ cat /etc/hostname
server.example.com# Display multiple files (concatenates them)[user@host ~]$ cat file1.txt file2.txt
# Show line numbers[user@host ~]$ cat -n /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
...# Show non-printing characters (useful for debugging)[user@host ~]$ cat -A file.txt
Line with tab^Iand end$# Number only non-empty lines[user@host ~]$ cat -b file.txt
Caution:cat on a large file (like a log) floods your terminal. Use less for big files.
Viewing Files: less
less is a pager - it displays files one screen at a time, letting you scroll forward and backward. Essential for large files.
# Open file in less[user@host ~]$ less /var/log/messages
Key
Action
Key
Action
Space
Next page
b
Previous page
↓ or j
Down one line
↑ or k
Up one line
g
Go to beginning
G
Go to end
/pattern
Search forward
?pattern
Search backward
n
Next search match
N
Previous match
q
Quit
h
Help
Remember: Press q to quit less. Man pages use less, so these keys work there too!
Viewing Files: head and tail
# Show first 10 lines (default)[user@host ~]$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
...# Show first 5 lines[user@host ~]$ head -n 5 /etc/passwd
[user@host ~]$ head -5 /etc/passwd # Short form# Show last 10 lines (default)[user@host ~]$ tail /var/log/messages
# Show last 20 lines[user@host ~]$ tail -n 20 /var/log/secure
# Follow file in real-time (watch for new lines)[user@host ~]$ tail -f /var/log/messages
(new lines appear as they're written)Press Ctrl+C to stop# Follow multiple files[user@host ~]$ tail -f /var/log/messages /var/log/secure
tail -f is essential for monitoring logs in real-time. Watch services start, troubleshoot errors, and observe system activity live.
Other Viewing Commands
# wc - word count (lines, words, characters)[user@host ~]$ wc /etc/passwd
45 89 2567 /etc/passwd[user@host ~]$ wc -l /etc/passwd # Lines only45 /etc/passwd# grep - search for patterns[user@host ~]$ grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash[user@host ~]$ grep -n "error" /var/log/messages # With line numbers[user@host ~]$ grep -i "ERROR" /var/log/messages # Case insensitive# diff - compare two files[user@host ~]$ diff file1.txt file2.txt
3c3
< old line three
---
> new line three# file - determine file type[user@host ~]$ file /etc/passwd
/etc/passwd: ASCII text[user@host ~]$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable...
Creating Files: Redirection
Output redirection sends command output to a file instead of the screen. Use > to create/overwrite, >> to append.
# Create file with echo[user@host ~]$ echo "Hello, World!" > greeting.txt
[user@host ~]$ cat greeting.txt
Hello, World!# Append to existing file[user@host ~]$ echo "Second line" >> greeting.txt
[user@host ~]$ cat greeting.txt
Hello, World!
Second line# WARNING: > overwrites without asking![user@host ~]$ echo "Oops" > greeting.txt # Previous content GONE# Create empty file with redirection[user@host ~]$ > emptyfile.txt
# Redirect command output to file[user@host ~]$ ls -la > directory_listing.txt
[user@host ~]$ date > timestamp.txt
Warning:> destroys existing content! Use >> to append safely.
Creating Files: Here Documents
Here documents let you create multi-line content inline. The content between the markers becomes the input.
Common use: Here documents are perfect for creating configuration files, scripts, and multi-line content in shell scripts.
Introduction to vim
vim (Vi IMproved) is the standard text editor on RHEL and most Linux systems. It is powerful, efficient, and available everywhere - but has a learning curve.
Why Learn vim?
Available on every Linux system Works over SSH Extremely powerful editing Fast once mastered Required for RHCSA exam
The Challenge
Modal editor (modes matter) Non-intuitive for beginners Keys do different things in different modes No on-screen menus Must learn commands
# Open or create a file with vim[user@host ~]$ vim filename.txt
# vi is often aliased to vim[user@host ~]$ vi filename.txt
vim Modes
vim is a modal editor. Keys behave differently in each mode. Understanding modes is the key to using vim.
Mode
Purpose
Enter With
Exit With
NORMAL
Navigation, commands, operators
Esc
(Default mode)
INSERT
Typing and inserting text
i, a, o
Esc
COMMAND
Save, quit, search, substitute
:
Enter or Esc
VISUAL
Select text for operations
v, V
Esc
vim - modes illustration
This is some text in the file. The cursor is blinking here._
NORMAL filename.txt [+] 2,25 All
Lost? Press Esc repeatedly to return to Normal mode. This is your safe haven.
vim Essential Commands
Starting and Quitting
Entering Insert Mode
:w
Save (write)
i
Insert before cursor
:q
Quit (fails if unsaved)
a
Append after cursor
:wq
Save and quit
o
Open line below
:q!
Quit without saving
O
Open line above
:wq!
Force save and quit
A
Append at end of line
Navigation (Normal Mode)
More Navigation
h j k l
Left, down, up, right
0
Beginning of line
w
Next word
$
End of line
b
Previous word
gg
First line of file
Ctrl+f
Page forward
G
Last line of file
Ctrl+b
Page backward
:42
Go to line 42
Minimum survival:i to type, Esc when done, :wq to save and quit.
vim Editing Commands
Command
Action
Command
Action
x
Delete character under cursor
dd
Delete entire line
dw
Delete word
d$
Delete to end of line
yy
Yank (copy) line
yw
Yank word
p
Paste after cursor
P
Paste before cursor
u
Undo last change
Ctrl+r
Redo
r
Replace single character
cw
Change word (delete + insert)
# Combining commands with counts5dd - Delete 5 lines
3yy - Yank 3 lines
10j - Move down 10 lines
d5w - Delete 5 words
2p - Paste 2 times# Repeat last change. - Repeat last editing command
# Search forward/pattern - Search forward for "pattern"
n - Next match
N - Previous match# Search backward?pattern - Search backward for "pattern"# Search and replace (substitute):s/old/new/ - Replace first "old" with "new" on current line
:s/old/new/g - Replace ALL "old" with "new" on current line
:%s/old/new/g - Replace ALL occurrences in entire file
:%s/old/new/gc - Replace all, but ask for Confirmation each time# Examples:%s/http/https/g - Change http to https everywhere
:%s/^#/;/g - Change comment character from # to ;
:5,15s/foo/bar/g - Replace only on lines 5-15
The substitute pattern::[range]s/search/replace/[flags]
Flags: g = global (all matches), c = confirm each, i = case insensitive
vim Visual Mode
Visual mode lets you select text visually, then apply operations to the selection. More intuitive for some tasks.
Key
Selection Type
v
Character-wise selection (highlight characters)
V
Line-wise selection (highlight entire lines)
Ctrl+v
Block selection (rectangular region)
# Visual mode workflow:1. Position cursor at start of selection
2. Press v (or V for lines)
3. Move cursor to extend selection (highlighted)
4. Press operator: d (delete), y (yank), c (change), etc.# Examples:V5jd - Select 5 lines down, delete them
viw - Select inner word (visual-inner-word)
va" - Select including quotes (visual-around-")
Vjj:s/old/new/g - Substitute only in selected lines
Tip: Visual mode is great when you're not sure exactly what you want to select. See it highlighted before acting.
vim Configuration
# Useful settings (use during session or add to ~/.vimrc):set number - Show line numbers
:set nonumber - Hide line numbers
:set hlsearch - Highlight search matches
:set nohlsearch - Remove highlights
:set ignorecase - Case-insensitive search
:set smartcase - Case-sensitive if uppercase used
:set tabstop=4 - Tab width of 4 spaces
:set expandtab - Use spaces instead of tabs
:set autoindent - Auto-indent new lines
:set syntax=on - Enable syntax highlighting# Create persistent configuration[user@host ~]$ vim ~/.vimrc
set number
set hlsearch
set ignorecase
set smartcase
set tabstop=4
set expandtab
set autoindent
syntax on
~/.vimrc is loaded every time vim starts. Put your preferred settings there for permanent configuration.
vim Practice Workflow
vim workflow example
1.Open file:vim /etc/hosts 2.Navigate to line::5(go to line 5) 3.Go to end of line:$ 4.Enter insert mode:a(append after cursor) 5.Type your text... 6.Return to normal:Esc 7.Save and quit::wq
:wq
Practice tip: Create a test file and experiment. Use :q! to quit without saving your experiments.
# Built-in vim tutorial (highly recommended!)[user@host ~]$ vimtutor
# Takes about 30 minutes, teaches all basics interactively
Introduction to nano
nano is a simple, user-friendly text editor. It displays keyboard shortcuts on screen and works like a typical editor - no modes.
GNU nano 5.6 - config.txt
server=192.168.1.100 port=8080 enabled=true
^G Help ^O Write Out ^W Where Is ^K Cut ^U Paste ^X Exit
# Open or create file with nano[user@host ~]$ nano filename.txt
# Open at specific line number[user@host ~]$ nano +25 filename.txt
The ^ symbol means Ctrl. So ^X means Ctrl+X. All shortcuts are shown at the bottom.
nano Essential Commands
Shortcut
Action
Shortcut
Action
Ctrl+O
Write Out (save)
Ctrl+X
Exit
Ctrl+K
Cut line
Ctrl+U
Paste (uncut)
Ctrl+W
Where Is (search)
Ctrl+\
Search and replace
Ctrl+G
Help
Ctrl+C
Show cursor position
Ctrl+A
Beginning of line
Ctrl+E
End of line
Ctrl+Y
Page up
Ctrl+V
Page down
Alt+U
Undo
Alt+E
Redo
Ctrl+_
Go to line number
Alt+G
Go to line (also)
# Common workflow:1. nano filename.txt - Open file
2. (make edits directly - just type)
3. Ctrl+O, Enter - Save
4. Ctrl+X - Exit# Search and replace:1. Ctrl+\ - Start replace
2. Enter search term, Enter
3. Enter replacement, Enter
4. A (for all) or Y/N for each
vim vs nano
vim Strengths
Extremely powerful editing
Very fast once learned
Available everywhere
Required for RHCSA exam
Macro recording
Extensive customization
Plugin ecosystem
nano Strengths
Immediately usable
On-screen help
No learning curve
Good for quick edits
Familiar interface
Less cognitive load
Lower risk of mistakes
Recommendation: Learn vim well - it is a career skill. Use nano for quick, simple edits when vim's power is overkill.
# Set your default editor (optional)[user@host ~]$ export EDITOR=vim
[user@host ~]$ echo 'export EDITOR=vim' >> ~/.bashrc
Key Takeaways
1
Viewing:cat for small files, less for paging, head/tail for portions. tail -f follows logs.
2
Creating:> redirects output to files. >> appends. Here documents for multi-line content.
3
vim: Modal editor: Normal (commands), Insert (typing), Command (:). i to type, Esc to stop, :wq to save.
4
nano: Simple editor with on-screen help. Ctrl+O saves, Ctrl+X exits. Good for quick edits.
Graded Lab
View /etc/passwd with cat, less, head, and tail
Create a configuration file using echo and redirection
Create a multi-line file using a here document
Complete vimtutor (run: vimtutor)
Edit a file in vim: navigate, insert text, save, quit