Introduction
This lesson is about small text tools.
The main question is:
How do you take text input, change it, shorten it, sort it, or inspect it without opening a big editor?
What you should be able to do after this lesson:
- Use common text filters.
- Read only the beginning or end of a stream.
- Sort and deduplicate lines.
- Transform characters.
- Check file checksums.
Big Idea: Small Tools, Simple Jobs
UNIX-style work is often:
one tool does one small text job -> pipe it to the next tool
That is why filters matter so much.
Basic Viewers
Print a whole file
cat file.txt
Read page by page
less file.txt
First lines
head file.txt
head -n 20 file.txt
Last lines
tail file.txt
tail -n 20 file.txt
Counting and Numbering
Count lines, words, bytes
wc file.txt
wc -l file.txt
Add line numbers
nl file.txt
Selecting and Rearranging Text
Select fields
cut -d: -f1 /etc/passwd
Example output:
root
bin
daemon
mail
ftp
http
nobody
dbus
What this shows:
cut -d: -f1extracts the first colon-separated field- on
/etc/passwd, that first field is the username
Sort lines
sort file.txt
Show unique lines
uniq file.txt
sort file.txt | uniq
Important beginner rule:
uniqonly removes adjacent duplicates- that is why
sort | uniqis so common
Join lines side by side
paste file1.txt file2.txt
Character Changes
Translate characters
echo "hello" | tr 'a-z' 'A-Z'
Example output:
HELLO
What this shows:
trcan translate one set of characters into another- uppercase conversion is one of the most common first examples
Simple text editing with sed
echo "color" | sed 's/color/colour/'
Splitting Files
split -l 100 bigfile.txt chunk_
This creates smaller files from a large input.
Inspecting Data
Show bytes in another form
od -c file.bin | head
This is helpful when a file is not plain text.
Checksums
Checksums help confirm file integrity.
md5sum file.iso
sha256sum file.iso
sha512sum file.iso
Reading Compressed Text Without Fully Extracting
zcat file.gz
bzcat file.bz2
xzcat file.xz
This is useful when you want to inspect content quickly.
Practice Step by Step
1) Count and sort usernames from `/etc/passwd`
- Extract usernames:
cut -d: -f1 /etc/passwd - Sort them:
cut -d: -f1 /etc/passwd | sort - Count them:
cut -d: -f1 /etc/passwd | wc -l
2) Convert text to uppercase
- Run:
echo "linux" | tr 'a-z' 'A-Z' - Result:
LINUX
Cheat Sheet
cat= print file contentless= view long text page by pagehead= first linestail= last lineswc= count lines, words, bytesnl= number linescut= select fieldssort= sort linesuniq= collapse adjacent duplicatespaste= join lines side by sidetr= translate characterssed= simple stream editingsplit= split large filesod= inspect raw datamd5sum,sha256sum,sha512sum= checksumszcat,bzcat,xzcat= read compressed text
🎯
Test Your Knowledge
Complete the quiz to assess your understanding of this course's concepts.
