Peters School of Business · Assiniboine College · NETW-0001
2 hours · Lab 2 Parts 3–5 · find, grep, sudo, text files, cp, mv, df, du
Path challenge and command warm-up
/ (those are absolute).ls -la show that ls alone doesn't?" — long format and hidden files. What makes a file hidden in Linux? (Answer: the filename starts with a dot. No special file attribute like Windows.)sudo — and today's lab introduces it.find, grep, sudo — three tools that make the CLI genuinely powerful
sudo — borrowing superuser privilege (5 min)
sudo (superuser do) lets an authorised user run a single command with root-level privilege. It's not the same as logging in as root — it's targeted, logged, and audited. Every sudo command is written to the system log.sudo is command-specific, explicit, and logged. UAC is application-specific and gives the whole application elevated rights for its session.sudo? Only users in the sudo group (on Ubuntu) or listed in /etc/sudoers. The student account was created during installation and is in the sudo group automatically. The lab explicitly requires sudo for Parts 3 onwards — this is intentional. Some of the directories we search belong to root.find — locate files anywhere on the system (8 min)
find recursively searches a directory tree for files matching criteria. Unlike Windows Search (which indexes), find walks the filesystem in real time — it's slower but always current and works on any Linux system without configuration.find [where] [what criteria] [what to do]. The "where" is required; criteria and action are optional (no criteria = list everything).| Flag | What it matches | Example |
|---|---|---|
-name "pattern" | Filename — case sensitive. Use -iname for case-insensitive | find / -name "beh" |
-size +500M | Files larger than 500 MB. Use - for smaller than, no sign for exact | find / -size +500M |
-mmin -10 | Modified in the last 10 minutes. -mtime for days | find / -name "*.log" -mmin -10 |
-type f | Files only. -type d for directories | find /etc -type f -name "*.conf" |
2>/dev/null | Not a find flag — redirect errors to nowhere. Suppresses "Permission denied" noise | find / -name "beh" 2>/dev/null |
grep — search inside files (7 min)
grep searches file contents for a matching string or pattern and prints every line that matches. Where find locates files by their properties, grep locates lines by what they contain. Bridge: Ctrl+F in a text editor — grep is the command-line version, but it can search hundreds of files at once.grep [options] "pattern" [file(s)]| Flag | Effect | Example |
|---|---|---|
-i | Case-insensitive match | grep -i "student" /etc/passwd |
-r | Recursive — search all files in a directory tree | grep -r "root" /etc/ |
-n | Show line numbers | grep -n "bash" /etc/shells |
-c | Count matching lines instead of printing them | grep -c "bash" /etc/shells |
-v | Invert — show lines that do NOT match | grep -v "nologin" /etc/passwd |
* wildcard | Search multiple files matching a pattern | grep "root" /etc/pass* |
cat /etc/passwd | grep "student" is equivalent to grep "student" /etc/passwd — but the pipe approach lets you chain further. cat ~/.bash_history | grep "find" | wc -l counts how many times you've run the find command. You'll use this exact pattern in today's lab.Lab 2 — Searching, text files, file operations, and disk usage
Parts 3, 4, and 5 of Lab 2, building on the directories created yesterday. The lab work today produces several files that will persist on the VM — don't delete them, they may be referenced in later labs.
Part 3 — Searching the filesystem with find and grep (25 min)
sudo — some directories are owned by root and cannot be searched without elevated privilege. Append 2>/dev/null to find commands to suppress permission-denied errors that clutter the output.sudo find / -name "beh" 2>/dev/null
sudo find / -size 10001c -readable 2>/dev/null (c = bytes in find)
*.log files modified in the last 10 minutes: sudo find / -name "*.log" -mmin -10 2>/dev/null
sudo find / -size +500M 2>/dev/null
/etc/passwd: grep "student" /etc/passwd. Identify and record: username, UID, GID, home directory, default shell — all are in that one line.
/etc/pass*: grep "root" /etc/pass*. What appears at the start of each output line when grep searches multiple files? (Answer: the filename, followed by a colon. This is grep telling you which file each match came from.)
/etc/shells contain the word bash: grep -c "bash" /etc/shells
find command so far this session: cat ~/.bash_history | grep "find" | wc -l. Record the number — it should match roughly how many find commands you've run today.
Part 4 — Creating and manipulating text files (30 min)
echo "What did the limestone say to the geologist?" > ~/lab2/lab2.question.txt
echo "Don't take me for granite!" > ~/lab2/lab2.answer.txt
ls -l ~/lab2/
cat ~/lab2/lab2.answer.txt — outputs the entire file to the terminal
less ~/lab2/lab2.answer.txt — opens in a pager (navigate with arrows, quit with q)
more ~/lab2/lab2.answer.txt — similar to less, older tool
ls -l ~/lab2/ and record: who owns lab2.answer.txt? What group is assigned to it? (Should be student:student — you created it while logged in as student.)file command to identify file types — record all three results:
file ~/lab2/lab2.question.txt — what type is it?
file ~/lab2/backups (the directory from yesterday) — what type is a directory?
file /usr/bin/grep — what type is grep? (It should say ELF executable — a compiled binary.)
man grep to find the flag that searches multiple files. Then search both lab files for the word "me". Record the exact command you used.cp ~/lab2/lab2.question.txt ~/lab2/lab2.question.bak and the same for lab2.answer.txt
mv ~/lab2/lab2.question.bak ~/lab2/backups/ and same for answer.bak
ls ~/lab2/ and ls ~/lab2/backups/
cat command can concatenate multiple files. Find a way to output both lab2.question.txt and lab2.answer.txt into a single new file called lab2.joke.txt, using one command. Record what you used. (Hint: cat file1 file2 > newfile)Part 5 — Disk usage tools (20 min)
df -Th — disk filesystem usage, human-readable, with filesystem types listed (-T = type, -h = human-readable). Record: how much space are you currently using? How much is free?
du -h --max-depth=1 ~ — directory sizes in your home folder, one level deep. Which directory uses the most space?
du -h --max-depth=1 / (with sudo) — which directory in the root folder uses the most space?
du command result for your home directory. Do they match? Record any differences and suggest why they might differ.Piping, wc, and the man page habit
/var/log: sudo find /var/log -type f -size +0c 2>/dev/null | xargs sudo ls -lhS 2>/dev/null | head -6. Break down each part — what does each command contribute to the pipeline?man grep — navigate with arrow keys, search with /pattern, quit with q. Find the flag that inverts the match (shows lines that do NOT contain the pattern). Use it: grep -v "nologin" /etc/passwd — what does this show?Tomorrow is the first mini-assessment — what to expect
Learning outcomes — by end of Day 4, students can…
What to have ready before class