DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

Linux Globbing — FULL Exercise Set

0️⃣ Setup (Run Once)

Create a safe practice directory.

mkdir ~/globbing-lab
cd ~/globbing-lab
touch file1.txt file2.txt file10.txt fileA.txt fileB.log fileC.log
touch app.log app1.log app2.log error.log
mkdir dir1 dir2 dirA
Enter fullscreen mode Exit fullscreen mode

1️⃣ * — Match ANY number of characters

Concept

Matches zero or more characters

Exercises

ls *
ls *.txt
ls *.log
ls file*
ls app*
Enter fullscreen mode Exit fullscreen mode

Expected understanding

  • * expands before command runs
  • Matches filenames, not file content

2️⃣ ? — Match EXACTLY ONE character

Concept

Matches one and only one character

Exercises

ls file?.txt
ls app?.log
ls dir?
Enter fullscreen mode Exit fullscreen mode

Try and think

ls file??.txt
Enter fullscreen mode Exit fullscreen mode

Why does file10.txt match?


3️⃣ [abc] — Match ONE character from a set

Concept

Matches any ONE character inside brackets

Exercises

ls file[12].txt
ls file[A-Z].txt
ls dir[12]
Enter fullscreen mode Exit fullscreen mode

Practical DevOps usage

rm app[12].log
Enter fullscreen mode Exit fullscreen mode

Deletes app1.log and app2.log


4️⃣ [a-z] — Match a RANGE

Concept

Alphabetical or numeric range

Exercises

ls file[a-z].txt
ls file[0-9].txt
ls dir[a-z]
Enter fullscreen mode Exit fullscreen mode

5️⃣ [!abc] — NEGATION (NOT match)

Concept

Matches anything NOT in brackets

Exercises

ls file[!1].txt
ls file[!0-9].txt
ls *.log
ls [!a]*.log
Enter fullscreen mode Exit fullscreen mode

6️⃣ {} — Brace Expansion (NOT globbing, but related)

⚠️ Happens before globbing

Exercises

touch {dev,stage,prod}.env
ls *.env
Enter fullscreen mode Exit fullscreen mode

Multiple patterns at once:

ls file{1,2}.txt
Enter fullscreen mode Exit fullscreen mode

7️⃣ Combine Globs (Real-world patterns)

Exercises

ls file[0-9]*.txt
ls app?.log
ls *[A-Z].*
Enter fullscreen mode Exit fullscreen mode

8️⃣ Recursive Globbing ** (Advanced)

Enable first

shopt -s globstar
Enter fullscreen mode Exit fullscreen mode

Create nested structure

mkdir -p logs/2024/app
touch logs/2024/app/app.log
Enter fullscreen mode Exit fullscreen mode

Exercise

ls **/*.log
Enter fullscreen mode Exit fullscreen mode

9️⃣ Hidden Files (.*)

Create hidden files

touch .env .gitignore .bashrc
Enter fullscreen mode Exit fullscreen mode

Exercises

ls .*
ls -a
Enter fullscreen mode Exit fullscreen mode

⚠️ * does NOT match hidden files by default


🔟 Globbing with Commands (Critical DevOps Skill)

Copy

cp *.log /tmp/
Enter fullscreen mode Exit fullscreen mode

Delete safely

rm -i file*.txt
Enter fullscreen mode Exit fullscreen mode

Count files

ls *.log | wc -l
Enter fullscreen mode Exit fullscreen mode

1️⃣1️⃣ Quoting DISABLES Globbing (Important)

Exercise

echo *.log
echo "*.log"
Enter fullscreen mode Exit fullscreen mode

Explain:

  • Quotes prevent shell expansion

1️⃣2️⃣ When GLOB DOES NOT MATCH

Try

ls nomatch*
Enter fullscreen mode Exit fullscreen mode

Enable safer behavior

shopt -s nullglob
ls nomatch*
Enter fullscreen mode Exit fullscreen mode

1️⃣3️⃣ Exam / Interview Questions (Very Common)

Q1

What is the difference between * and ??

Q2

Why does rm *.log sometimes delete too much?

Q3

Why doesn’t ls * show .env?

Q4

Difference between {} and []?


1️⃣4️⃣ Mini Lab (Assignment)

  1. Delete all .log files except error.log
  2. List only files ending with numbers
  3. Copy only file1.txt and file2.txt using ONE command
  4. List everything except .txt files

1️⃣5️⃣ Real DevOps Usage Examples

kubectl logs pod-* 
aws s3 rm s3://bucket/logs/2024/*
scp app*.jar server:/opt/apps/
Enter fullscreen mode Exit fullscreen mode

✅ Summary Table

Pattern Meaning
* Any characters
? One character
[abc] One from set
[a-z] Range
[!abc] Not in set
{a,b} Expansion
** Recursive
.* Hidden files

find Command — ? Meaning & Exercises (Ubuntu)

1️⃣ What does ? mean in find?

👉 ? is a wildcard that matches EXACTLY ONE character
It is used inside -name or -iname.

Important:

  • ? is evaluated by find, not by the shell
  • It matches one character only

2️⃣ Setup (Run Once)

mkdir ~/find-lab
cd ~/find-lab
touch file1.txt file2.txt file10.txt fileA.txt fileB.log
mkdir dir1 dir2 dir10
Enter fullscreen mode Exit fullscreen mode

3️⃣ Basic find with ?

Find files with ONE character after file

find . -name "file?.txt"
Enter fullscreen mode Exit fullscreen mode

✔ Matches:

  • file1.txt
  • file2.txt
  • fileA.txt

❌ Does NOT match:

  • file10.txt (because 10 = two characters)

4️⃣ Compare ? vs * (Very Important)

Using ?

find . -name "file?.txt"
Enter fullscreen mode Exit fullscreen mode

Using *

find . -name "file*.txt"
Enter fullscreen mode Exit fullscreen mode

Difference:

  • ? → exactly 1 character
  • *any number of characters

5️⃣ ? with directories

find . -type d -name "dir?"
Enter fullscreen mode Exit fullscreen mode

✔ Matches:

  • dir1
  • dir2

❌ Does NOT match:

  • dir10

6️⃣ Multiple ? characters

find . -name "file??.txt"
Enter fullscreen mode Exit fullscreen mode

✔ Matches:

  • file10.txt

Explanation:

  • ?? = exactly two characters

7️⃣ Case-Insensitive search (-iname)

find . -iname "file?.txt"
Enter fullscreen mode Exit fullscreen mode

Matches:

  • fileA.txt
  • filea.txt (if exists)

8️⃣ Real DevOps-style exercises

1️⃣ Find log files with one-digit version

find /var/log -name "app?.log"
Enter fullscreen mode Exit fullscreen mode

2️⃣ Delete files carefully using ?

find . -name "file?.txt" -delete
Enter fullscreen mode Exit fullscreen mode

⚠ Deletes only:

  • file1.txt
  • file2.txt
  • fileA.txt

3️⃣ Count files matching ?

find . -name "file?.txt" | wc -l
Enter fullscreen mode Exit fullscreen mode

4️⃣ Combine ? with type

find . -type f -name "file?.*"
Enter fullscreen mode Exit fullscreen mode

9️⃣ Common Mistake (Interview Question)

❌ WRONG:

find . -name file?.txt
Enter fullscreen mode Exit fullscreen mode

✔ CORRECT:

find . -name "file?.txt"
Enter fullscreen mode Exit fullscreen mode

Why?

  • Without quotes, shell may expand ? before find runs

🔟 Interview Questions

Q1: Difference between ? in ls and find?
Answer:

  • ls → shell expands
  • find → find evaluates pattern

Q2: Why does file10.txt not match file?.txt?
Answer:
Because ? matches exactly one character


✅ Summary

Pattern Meaning
? Exactly one character
?? Exactly two characters
* Any number of characters

head and tail Commands — Full Explanation + Exercises

1️⃣ What is head?

👉 head shows the first lines of a file.

Default behavior

head file.txt
Enter fullscreen mode Exit fullscreen mode

✔ Shows first 10 lines


2️⃣ What is tail?

👉 tail shows the last lines of a file.

Default behavior

tail file.txt
Enter fullscreen mode Exit fullscreen mode

✔ Shows last 10 lines


3️⃣ Setup (Run Once)

mkdir ~/head-tail-lab
cd ~/head-tail-lab
seq 1 50 > numbers.txt
Enter fullscreen mode Exit fullscreen mode

This file has 50 lines.


4️⃣ head Exercises

Show first 5 lines

head -n 5 numbers.txt
Enter fullscreen mode Exit fullscreen mode

Show first 1 line

head -n 1 numbers.txt
Enter fullscreen mode Exit fullscreen mode

Shortcut

head -5 numbers.txt
Enter fullscreen mode Exit fullscreen mode

5️⃣ tail Exercises

Show last 5 lines

tail -n 5 numbers.txt
Enter fullscreen mode Exit fullscreen mode

Show last line only

tail -n 1 numbers.txt
Enter fullscreen mode Exit fullscreen mode

6️⃣ tail -f (VERY IMPORTANT for DevOps)

👉 Follow a file in real time

Terminal 1

tail -f numbers.txt
Enter fullscreen mode Exit fullscreen mode

Terminal 2

echo "new log line" >> numbers.txt
Enter fullscreen mode Exit fullscreen mode

You will see output instantly.

Used for:

  • Logs
  • Kubernetes
  • Application debugging

7️⃣ tail -n +NUMBER (Start from line)

tail -n +10 numbers.txt
Enter fullscreen mode Exit fullscreen mode

✔ Shows from line 10 till end


8️⃣ Combine head and tail

Get line 10 only

head -n 10 numbers.txt | tail -n 1
Enter fullscreen mode Exit fullscreen mode

Get lines 10–15

head -n 15 numbers.txt | tail -n 6
Enter fullscreen mode Exit fullscreen mode

9️⃣ Real DevOps Log Examples

View last 50 lines of syslog

tail -n 50 /var/log/syslog
Enter fullscreen mode Exit fullscreen mode

Follow Docker logs

docker logs -f container_name
Enter fullscreen mode Exit fullscreen mode

Follow Kubernetes pod logs

kubectl logs -f pod-name
Enter fullscreen mode Exit fullscreen mode

🔟 Common Interview Questions

Q1

What is difference between tail and tail -f?

Answer:

  • tail → static output
  • tail -f → live streaming

Q2

How to get only the first line of a file?

head -n 1 file.txt
Enter fullscreen mode Exit fullscreen mode

Q3

How to get last line only?

tail -n 1 file.txt
Enter fullscreen mode Exit fullscreen mode

1️⃣1️⃣ Mini Lab (Homework)

  1. Create a file with 100 lines
  2. Print first 20 lines
  3. Print last 15 lines
  4. Show line number 50
  5. Follow file while adding new lines

✅ Summary Table

Command Purpose
head First lines
head -n N First N lines
tail Last lines
tail -n N Last N lines
tail -f Follow live logs
tail -n +N Start from line N

wc (Word Count) — Full Explanation + Exercises

1️⃣ What is wc?

👉 wc = word count
Used to count:

  • lines
  • words
  • characters / bytes

2️⃣ wc Output Explained

wc file.txt
Enter fullscreen mode Exit fullscreen mode

Output format:

LINES  WORDS  BYTES  filename
Enter fullscreen mode Exit fullscreen mode

Example:

50  50  141  file.txt
Enter fullscreen mode Exit fullscreen mode

3️⃣ Setup (Run Once)

mkdir ~/wc-lab
cd ~/wc-lab
seq 1 20 > numbers.txt
echo "hello linux devops world" > text.txt
Enter fullscreen mode Exit fullscreen mode

4️⃣ wc -l — Line Count (Most Used)

wc -l numbers.txt
Enter fullscreen mode Exit fullscreen mode

✔ Counts number of lines


5️⃣ wc -w — Word Count

wc -w text.txt
Enter fullscreen mode Exit fullscreen mode

Counts words separated by spaces


6️⃣ wc -c — Byte Count

wc -c text.txt
Enter fullscreen mode Exit fullscreen mode

Counts bytes (includes newline)


7️⃣ wc -m — Character Count

wc -m text.txt
Enter fullscreen mode Exit fullscreen mode

Better for multi-byte characters (UTF-8)


8️⃣ Using wc with Pipes (Very Important)

Count files

ls | wc -l
Enter fullscreen mode Exit fullscreen mode

Count log lines

tail -n 100 /var/log/syslog | wc -l
Enter fullscreen mode Exit fullscreen mode

Count matching lines

grep error /var/log/syslog | wc -l
Enter fullscreen mode Exit fullscreen mode

9️⃣ Real DevOps Use Cases

1️⃣ Count running pods

kubectl get pods | wc -l
Enter fullscreen mode Exit fullscreen mode

2️⃣ Count files in directory

find . -type f | wc -l
Enter fullscreen mode Exit fullscreen mode

3️⃣ Count users

cat /etc/passwd | wc -l
Enter fullscreen mode Exit fullscreen mode

🔟 Common Mistakes

❌ Thinking lc exists
✔ Correct is:

wc -l
Enter fullscreen mode Exit fullscreen mode

❌ Forgetting pipes

ls wc -l   # wrong
ls | wc -l # correct
Enter fullscreen mode Exit fullscreen mode

1️⃣1️⃣ Mini Lab (Homework)

  1. Count lines in /etc/passwd
  2. Count words in a file you create
  3. Count number of .log files
  4. Count how many users have /bin/bash
  5. Count errors in syslog

1️⃣2️⃣ Interview Questions

Q1: Difference between wc -c and wc -m
Answer:

  • -c → bytes
  • -m → characters

Q2: How to count output lines of a command?
Answer:
Use pipe:

command | wc -l
Enter fullscreen mode Exit fullscreen mode

✅ Summary Table

Option Meaning
wc Lines, words, bytes
wc -l Line count
wc -w Word count
wc -c Byte count
wc -m Character count

File Size Determination — Exercises (Ubuntu)

1️⃣ Setup (Run Once)

mkdir ~/file-size-lab
cd ~/file-size-lab
echo "hello world" > small.txt
seq 1 1000 > numbers.txt
dd if=/dev/zero of=bigfile.bin bs=1M count=5
mkdir logs
cp numbers.txt logs/
Enter fullscreen mode Exit fullscreen mode

2️⃣ ls -lh — Human-readable size (Most common)

Exercise

ls -lh
Enter fullscreen mode Exit fullscreen mode

✔ Shows file sizes in:

  • B
  • KB
  • MB
  • GB

Questions

  • Which file is the largest?
  • What size is bigfile.bin?

3️⃣ du -h — Disk usage (Real storage used)

Exercise

du -h
Enter fullscreen mode Exit fullscreen mode

File only

du -h small.txt
Enter fullscreen mode Exit fullscreen mode

Directory size

du -h logs/
Enter fullscreen mode Exit fullscreen mode

Summary only

du -sh logs/
Enter fullscreen mode Exit fullscreen mode

du = how much disk space is actually used


4️⃣ stat — Exact size in bytes (Interview favorite)

Exercise

stat numbers.txt
Enter fullscreen mode Exit fullscreen mode

Look for:

Size: 3893
Enter fullscreen mode Exit fullscreen mode

✔ Most accurate size info


5️⃣ wc -c — Size in bytes (Content-based)

Exercise

wc -c small.txt
Enter fullscreen mode Exit fullscreen mode

✔ Counts bytes including newline

Compare with:

stat small.txt
Enter fullscreen mode Exit fullscreen mode

6️⃣ Compare Commands (Important Concept)

Command What it shows
ls -lh Display size
du -h Disk usage
stat Exact metadata
wc -c Content size

7️⃣ Find Files Bigger Than X Size

Bigger than 1MB

find . -type f -size +1M
Enter fullscreen mode Exit fullscreen mode

Smaller than 1KB

find . -type f -size -1k
Enter fullscreen mode Exit fullscreen mode

8️⃣ Sort Files by Size

Largest first

ls -lhS
Enter fullscreen mode Exit fullscreen mode

Smallest first

ls -lhrS
Enter fullscreen mode Exit fullscreen mode

9️⃣ Real DevOps Exercises

1️⃣ Find large log files

find /var/log -type f -size +100M
Enter fullscreen mode Exit fullscreen mode

2️⃣ Check directory disk usage

du -sh /var/log
Enter fullscreen mode Exit fullscreen mode

3️⃣ Monitor growing file

watch -n 1 ls -lh bigfile.bin
Enter fullscreen mode Exit fullscreen mode

🔟 Common Mistakes (Interview)

❌ Using ls to check disk usage
✔ Use du for actual disk usage

❌ Confusing bytes vs blocks
stat is the source of truth


1️⃣1️⃣ Mini Lab (Homework)

  1. Create a file of 10MB
  2. Show its size using 4 different commands
  3. Find files larger than 1MB
  4. Sort files by size
  5. Check disk usage of a directory

1️⃣2️⃣ Interview Questions

Q1: Difference between ls -lh and du -h?
Answer:

  • ls → file size
  • du → disk blocks used

Q2: Which command gives exact size in bytes?
Answer:
stat


✅ Summary Cheat Sheet

ls -lh file
du -sh file_or_dir
stat file
wc -c file
find . -size +10M
Enter fullscreen mode Exit fullscreen mode

Output Redirection Exercises — > and >>

1️⃣ What are > and >>?

Operator Meaning
> Redirect output and overwrite file
>> Redirect output and append to file

2️⃣ Setup (Run Once)

mkdir ~/redirect-lab
cd ~/redirect-lab
Enter fullscreen mode Exit fullscreen mode

3️⃣ > — Overwrite Redirection

Exercise 1

echo "Hello DevOps" > output.txt
cat output.txt
Enter fullscreen mode Exit fullscreen mode

✔ File created


Exercise 2 (Overwrite)

echo "Linux is powerful" > output.txt
cat output.txt
Enter fullscreen mode Exit fullscreen mode

❗ Previous content is deleted


4️⃣ >> — Append Redirection

Exercise 3

echo "Line 1" >> output.txt
echo "Line 2" >> output.txt
cat output.txt
Enter fullscreen mode Exit fullscreen mode

✔ Content is added, not replaced


5️⃣ Real Difference Test (Very Important)

echo "START" > test.txt
echo "APPEND" >> test.txt
echo "OVERWRITE" > test.txt
cat test.txt
Enter fullscreen mode Exit fullscreen mode

Expected result:

OVERWRITE
Enter fullscreen mode Exit fullscreen mode

6️⃣ Redirect Command Output

Save ls output

ls > files.txt
Enter fullscreen mode Exit fullscreen mode

Append more output

ls /etc >> files.txt
Enter fullscreen mode Exit fullscreen mode

7️⃣ Redirect Errors (stderr)

Error without redirect

ls not_exist
Enter fullscreen mode Exit fullscreen mode

Redirect error

ls not_exist 2> error.txt
Enter fullscreen mode Exit fullscreen mode

Append error

ls not_exist 2>> error.txt
Enter fullscreen mode Exit fullscreen mode

8️⃣ Redirect Both Output and Error

ls /etc not_exist > all.txt 2>&1
Enter fullscreen mode Exit fullscreen mode

9️⃣ Discard Output (Production Trick)

ls not_exist > /dev/null
ls not_exist 2> /dev/null
ls not_exist > /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

🔟 Combine with wc, tail, grep

Count lines and save

ls /etc | wc -l > count.txt
Enter fullscreen mode Exit fullscreen mode

Append logs

date >> app.log
Enter fullscreen mode Exit fullscreen mode

1️⃣1️⃣ Real DevOps Examples

Log rotation simulation

echo "$(date) App started" >> app.log
Enter fullscreen mode Exit fullscreen mode

Save command result in script

df -h > disk_report.txt
Enter fullscreen mode Exit fullscreen mode

1️⃣2️⃣ Common Mistakes (Interview)

❌ Using > instead of >> → data loss
❌ Forgetting 2> for errors
❌ Overwriting log files accidentally


1️⃣3️⃣ Mini Lab (Homework)

  1. Create report.txt using >
  2. Append 5 lines using >>
  3. Save ls /etc output
  4. Save errors separately
  5. Discard output using /dev/null

1️⃣4️⃣ Interview Questions

Q1: Difference between > and >>
Answer:

  • > overwrites
  • >> appends

Q2: How to redirect error only?
Answer:

2>
Enter fullscreen mode Exit fullscreen mode

✅ Summary Cheat Sheet

command > file
command >> file
command 2> error.txt
command > all.txt 2>&1
command > /dev/null
Enter fullscreen mode Exit fullscreen mode

tee Command — Exercises & Explanation

1️⃣ What is tee?

👉 tee reads from standard input and writes to:

  • the screen
  • and one or more files at the same time

Think of it as a splitter.

command | tee file.txt
Enter fullscreen mode Exit fullscreen mode

2️⃣ Setup (Run Once)

mkdir ~/tee-lab
cd ~/tee-lab
Enter fullscreen mode Exit fullscreen mode

3️⃣ Basic tee Exercise

Write output to screen AND file

echo "Hello DevOps" | tee output.txt
Enter fullscreen mode Exit fullscreen mode

Check:

cat output.txt
Enter fullscreen mode Exit fullscreen mode

4️⃣ tee -a (Append Mode)

Overwrite vs append

echo "Line 1" | tee output.txt
echo "Line 2" | tee -a output.txt
Enter fullscreen mode Exit fullscreen mode

-a = append
❌ Without -a = overwrite


5️⃣ tee vs > (Important Difference)

Using >

ls > files.txt
Enter fullscreen mode Exit fullscreen mode

❌ No output on screen

Using tee

ls | tee files.txt
Enter fullscreen mode Exit fullscreen mode

✔ Output shown AND saved


6️⃣ Multiple Files with tee

echo "Shared log line" | tee file1.log file2.log
Enter fullscreen mode Exit fullscreen mode

7️⃣ tee in Pipelines

Count and save

ls | tee list.txt | wc -l
Enter fullscreen mode Exit fullscreen mode

Analyze logs

grep error /var/log/syslog | tee errors.txt | wc -l
Enter fullscreen mode Exit fullscreen mode

8️⃣ sudo + tee (VERY IMPORTANT)

Problem

sudo echo "text" > /root/test.txt   # fails
Enter fullscreen mode Exit fullscreen mode

Solution

echo "text" | sudo tee /root/test.txt
Enter fullscreen mode Exit fullscreen mode

Append with sudo

echo "more text" | sudo tee -a /root/test.txt
Enter fullscreen mode Exit fullscreen mode

9️⃣ Real DevOps Use Cases

Save disk report

df -h | tee disk_report.txt
Enter fullscreen mode Exit fullscreen mode

Capture install logs

sudo apt update | tee update.log
Enter fullscreen mode Exit fullscreen mode

Kubernetes debugging

kubectl logs pod-name | tee pod.log
Enter fullscreen mode Exit fullscreen mode

🔟 Common Mistakes

❌ Forgetting -a → overwrites logs
❌ Using > with sudo
❌ Thinking tee replaces pipes


1️⃣1️⃣ Mini Lab (Homework)

  1. Save ls output and still see it
  2. Append 5 lines to a log file
  3. Write output to two files
  4. Save error logs and count them
  5. Write to a protected file using sudo

1️⃣2️⃣ Interview Questions

Q1: Why use tee instead of >?
Answer:
To see output and save it at the same time.

Q2: Why does sudo echo text > file fail?
Answer:
Redirection happens before sudo.


✅ Summary Cheat Sheet

command | tee file.txt
command | tee -a file.txt
command | tee file1 file2
command | sudo tee /path/file
Enter fullscreen mode Exit fullscreen mode

uniq and grep — Exercises & Explanation

1️⃣ What is grep?

👉 grep searches for patterns in text.

Basic syntax:

grep PATTERN file
Enter fullscreen mode Exit fullscreen mode

2️⃣ What is uniq?

👉 uniq removes or analyzes duplicate lines
⚠️ Important: uniq works only on adjacent lines → usually needs sort first.


3️⃣ Setup (Run Once)

mkdir ~/grep-uniq-lab
cd ~/grep-uniq-lab

cat <<EOF > logs.txt
error disk full
info service started
warning cpu high
error disk full
error network down
info service started
EOF
Enter fullscreen mode Exit fullscreen mode

4️⃣ grep — Basic Exercises

Find lines containing error

grep error logs.txt
Enter fullscreen mode Exit fullscreen mode

Case-insensitive search

grep -i ERROR logs.txt
Enter fullscreen mode Exit fullscreen mode

Show line numbers

grep -n error logs.txt
Enter fullscreen mode Exit fullscreen mode

Count matching lines

grep -c error logs.txt
Enter fullscreen mode Exit fullscreen mode

Search multiple patterns

grep -E "error|warning" logs.txt
Enter fullscreen mode Exit fullscreen mode

5️⃣ grep — Inverted Match (-v)

Show lines WITHOUT info

grep -v info logs.txt
Enter fullscreen mode Exit fullscreen mode

Used a lot in log filtering.


6️⃣ grep with Pipes (Real Usage)

Find errors in last 100 log lines

tail -n 100 /var/log/syslog | grep error
Enter fullscreen mode Exit fullscreen mode

Count errors

grep error logs.txt | wc -l
Enter fullscreen mode Exit fullscreen mode

7️⃣ uniq — Basic (Important Rule)

❌ Wrong (no effect)

uniq logs.txt
Enter fullscreen mode Exit fullscreen mode

Why?
👉 Duplicates are not adjacent


8️⃣ sort + uniq (Correct Way)

Remove duplicates

sort logs.txt | uniq
Enter fullscreen mode Exit fullscreen mode

Count duplicate lines

sort logs.txt | uniq -c
Enter fullscreen mode Exit fullscreen mode

Output example:

2 error disk full
2 info service started
1 warning cpu high
Enter fullscreen mode Exit fullscreen mode

9️⃣ uniq Options (Very Important)

Show only duplicates

sort logs.txt | uniq -d
Enter fullscreen mode Exit fullscreen mode

Show only unique (non-duplicate)

sort logs.txt | uniq -u
Enter fullscreen mode Exit fullscreen mode

🔟 Combine grep + uniq

Unique error messages

grep error logs.txt | sort | uniq
Enter fullscreen mode Exit fullscreen mode

Count unique errors

grep error logs.txt | sort | uniq -c
Enter fullscreen mode Exit fullscreen mode

1️⃣1️⃣ Real DevOps Examples

Count unique IPs

grep "GET" access.log | awk '{print $1}' | sort | uniq -c
Enter fullscreen mode Exit fullscreen mode

Unique Kubernetes errors

kubectl logs pod-name | grep error | sort | uniq -c
Enter fullscreen mode Exit fullscreen mode

1️⃣2️⃣ Common Mistakes (Interview)

❌ Using uniq without sort
❌ Forgetting -i for case-insensitive search
❌ Using grep instead of grep -v


1️⃣3️⃣ Mini Lab (Homework)

  1. Find all error lines
  2. Count how many times each error appears
  3. Show unique log messages
  4. Exclude info messages
  5. Find warnings OR errors

1️⃣4️⃣ Interview Questions

Q1: Why doesn’t uniq remove all duplicates?
Answer:
Because it works only on adjacent lines


Q2: Difference between grep error and grep -v error?
Answer:

  • grep error → matches
  • grep -v error → excludes

✅ Summary Cheat Sheet

grep pattern file
grep -i pattern file
grep -v pattern file
grep -c pattern file

sort file | uniq
sort file | uniq -c
sort file | uniq -d
Enter fullscreen mode Exit fullscreen mode

PATH Environment Variable — Explanation & Exercises

1️⃣ What is PATH?

👉 PATH is a colon-separated list of directories.
When you type a command, Linux searches these directories in order.

Example:

echo $PATH
Enter fullscreen mode Exit fullscreen mode

Output looks like:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Enter fullscreen mode Exit fullscreen mode

2️⃣ Why PATH Matters (Real Meaning)

When you run:

ls
Enter fullscreen mode Exit fullscreen mode

Linux actually checks:

/usr/local/sbin/ls
/usr/local/bin/ls
/usr/sbin/ls
/usr/bin/ls   ✅ found here
Enter fullscreen mode Exit fullscreen mode

3️⃣ Setup (Run Once)

mkdir -p ~/path-lab/bin
cd ~/path-lab/bin
Enter fullscreen mode Exit fullscreen mode

Create a custom command:

echo -e '#!/bin/bash\necho "Hello from custom command"' > hello
chmod +x hello
Enter fullscreen mode Exit fullscreen mode

4️⃣ Command NOT in PATH

Try running:

hello
Enter fullscreen mode Exit fullscreen mode

❌ Command not found

Run with full path:

~/path-lab/bin/hello
Enter fullscreen mode Exit fullscreen mode

✔ Works


5️⃣ Temporarily Add to PATH

export PATH=$PATH:$HOME/path-lab/bin
Enter fullscreen mode Exit fullscreen mode

Now run:

hello
Enter fullscreen mode Exit fullscreen mode

✔ Works without full path

⚠️ This change lasts only for this terminal session


6️⃣ Check Where Command Comes From

which ls
Enter fullscreen mode Exit fullscreen mode
which hello
Enter fullscreen mode Exit fullscreen mode

Shows exact path of the command being used.


7️⃣ Order of PATH (VERY IMPORTANT)

Create another command:

echo -e '#!/bin/bash\necho "Fake ls command"' > ls
chmod +x ls
Enter fullscreen mode Exit fullscreen mode

Run:

ls
Enter fullscreen mode Exit fullscreen mode

Why didn’t it run your fake one?

👉 Because your directory is after /usr/bin


8️⃣ Put Directory FIRST in PATH (Danger Demo)

export PATH=$HOME/path-lab/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

Now:

ls
Enter fullscreen mode Exit fullscreen mode

⚠️ Your fake ls runs!

👉 Security risk — interview favorite


9️⃣ Permanent PATH Change (User Only)

Edit:

nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Add:

export PATH=$PATH:$HOME/path-lab/bin
Enter fullscreen mode Exit fullscreen mode

Reload:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

🔟 System-wide PATH (Admins)

/etc/profile
/etc/environment
Enter fullscreen mode Exit fullscreen mode

⚠️ Requires sudo
⚠️ Affects all users


1️⃣1️⃣ PATH with Scripts (DevOps Use)

Script without path:

#!/bin/bash
aws s3 ls
Enter fullscreen mode Exit fullscreen mode

If AWS CLI not in PATH → script fails

Fix:

which aws
Enter fullscreen mode Exit fullscreen mode

Or use full path:

/usr/bin/aws s3 ls
Enter fullscreen mode Exit fullscreen mode

1️⃣2️⃣ Common Mistakes (Interview)

❌ Overwriting PATH:

export PATH=/my/bin   # WRONG
Enter fullscreen mode Exit fullscreen mode

✔ Correct:

export PATH=$PATH:/my/bin
Enter fullscreen mode Exit fullscreen mode

❌ Putting . in PATH (security risk)


1️⃣3️⃣ Mini Lab (Homework)

  1. Print your PATH
  2. Create a custom command
  3. Run it using full path
  4. Add directory to PATH
  5. Make change permanent
  6. Explain PATH order

1️⃣4️⃣ Interview Questions

Q1: What is PATH?
Answer:
A list of directories where shell looks for commands

Q2: Why is PATH order important?
Answer:
First match wins → security impact

Q3: Difference between temporary and permanent PATH?
Answer:

  • Temporary → export
  • Permanent → .bashrc

✅ Summary Cheat Sheet

echo $PATH
export PATH=$PATH:/new/path
which command
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Bash Variables & Environment Variables

Full Exercise Set for DevOps Engineers


1️⃣ Bash Variable (Local Variable)

What it is

  • Exists only in current shell
  • NOT available to child processes

Exercise

NAME="devops"
echo $NAME
Enter fullscreen mode Exit fullscreen mode

❌ Space is not allowed:

NAME = devops   # wrong
Enter fullscreen mode Exit fullscreen mode

2️⃣ Variable Scope Test (Very Important)

MYVAR="local"
bash
echo $MYVAR
Enter fullscreen mode Exit fullscreen mode

❌ Output is empty
👉 Local variables do not pass to child shells

Exit:

exit
Enter fullscreen mode Exit fullscreen mode

3️⃣ Environment Variable (export)

What it is

  • Available to child processes
  • Used heavily in DevOps tools

Exercise

export ENV="production"
echo $ENV
Enter fullscreen mode Exit fullscreen mode

Test in child shell:

bash
echo $ENV
exit
Enter fullscreen mode Exit fullscreen mode

✔ Works


4️⃣ Difference: Bash vs Env Variable

Type Child shell
Local variable ❌ No
Environment variable ✅ Yes

5️⃣ List Variables

All shell variables

set
Enter fullscreen mode Exit fullscreen mode

Only environment variables

env
Enter fullscreen mode Exit fullscreen mode

or

printenv
Enter fullscreen mode Exit fullscreen mode

6️⃣ Unset Variables

TEMP=123
unset TEMP
echo $TEMP
Enter fullscreen mode Exit fullscreen mode

✔ Empty output


7️⃣ Read-only Variables

readonly VERSION="1.0"
VERSION="2.0"
Enter fullscreen mode Exit fullscreen mode

❌ Permission denied


8️⃣ Command Substitution (VERY IMPORTANT)

Store command output in variable

DATE=$(date)
echo $DATE
Enter fullscreen mode Exit fullscreen mode

Old style (still seen):

DATE=`date`
Enter fullscreen mode Exit fullscreen mode

9️⃣ Variable Expansion Types

Default value

echo ${APP:-nginx}
Enter fullscreen mode Exit fullscreen mode

Assign default

echo ${APP:=nginx}
Enter fullscreen mode Exit fullscreen mode

Error if unset

echo ${APP:?APP is not set}
Enter fullscreen mode Exit fullscreen mode

🔟 Positional Variables (Scripts)

Create script:

nano args.sh
Enter fullscreen mode Exit fullscreen mode
#!/bin/bash
echo "Script: $0"
echo "First: $1"
echo "Second: $2"
echo "All: $@"
Enter fullscreen mode Exit fullscreen mode

Run:

chmod +x args.sh
./args.sh dev prod
Enter fullscreen mode Exit fullscreen mode

1️⃣1️⃣ Special Variables (INTERVIEW FAVORITE)

Variable Meaning
$0 Script name
$1..$9 Arguments
$# Number of args
$@ All args
$? Exit code
$$ PID
$! Last background PID

Exercise

ls
echo $?
Enter fullscreen mode Exit fullscreen mode

1️⃣2️⃣ Global Environment Files (DevOps MUST KNOW)

File Scope
~/.bashrc User, interactive
~/.profile Login shell
/etc/environment System-wide
/etc/profile System-wide

1️⃣3️⃣ Permanent Environment Variable

User-level

echo 'export APP_ENV=prod' >> ~/.bashrc
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

1️⃣4️⃣ Environment Variables in Scripts

#!/bin/bash
echo "Deploying to $APP_ENV"
Enter fullscreen mode Exit fullscreen mode

Run:

APP_ENV=stage ./script.sh
Enter fullscreen mode Exit fullscreen mode

1️⃣5️⃣ Inline Environment Variable (VERY COMMON)

DB_HOST=localhost DB_PORT=5432 ./app.sh
Enter fullscreen mode Exit fullscreen mode

✔ Variable exists only for this command


1️⃣6️⃣ .env File (Real DevOps Pattern)

Create:

nano .env
Enter fullscreen mode Exit fullscreen mode
DB_USER=admin
DB_PASS=secret
Enter fullscreen mode Exit fullscreen mode

Load:

export $(cat .env | xargs)
Enter fullscreen mode Exit fullscreen mode

Use:

echo $DB_USER
Enter fullscreen mode Exit fullscreen mode

1️⃣7️⃣ PATH Variable (Critical)

echo $PATH
export PATH=$PATH:/custom/bin
Enter fullscreen mode Exit fullscreen mode

⚠ Never overwrite PATH


1️⃣8️⃣ DevOps Tool Examples

Docker

export DOCKER_BUILDKIT=1
Enter fullscreen mode Exit fullscreen mode

Kubernetes

export KUBECONFIG=~/.kube/config
Enter fullscreen mode Exit fullscreen mode

AWS

export AWS_ACCESS_KEY_ID=xxx
export AWS_SECRET_ACCESS_KEY=yyy
export AWS_DEFAULT_REGION=us-east-1
Enter fullscreen mode Exit fullscreen mode

1️⃣9️⃣ Security Best Practices (IMPORTANT)

❌ Never hardcode secrets:

DB_PASS=admin123
Enter fullscreen mode Exit fullscreen mode

✔ Use env vars or secret managers

.env → add to .gitignore


2️⃣0️⃣ Mini DevOps Lab (Homework)

  1. Create local variable and test scope
  2. Export env variable and test child shell
  3. Create script using positional parameters
  4. Use $? to check command success
  5. Load variables from .env
  6. Set permanent env variable
  7. Use inline env variable

2️⃣1️⃣ Interview Questions (REAL)

Q: Difference between set and env
A:

  • set → shell variables
  • env → environment variables

Q: Why use env vars in DevOps?
A:
For configuration, secrets, portability

Q: How do you pass env vars to Docker/K8s?
A:
Environment variables or secret managers


✅ Final Cheat Sheet

VAR=value
export VAR=value
unset VAR
readonly VAR=value
VAR=$(command)
echo $?
env
set
Enter fullscreen mode Exit fullscreen mode

Top comments (0)