🧪 Linux File-Finding and Data-Sorting Exercise
🔍 Task 1: Find All Files in /home Without Errors
Run:
find /home -type f -print 2>/dev/nullProtocol Notes:
- What does
2>/dev/nulldo? - Why might errors occur without it?
- How many files were found?
📦 Task 2: Find Large Files on the System
Run:
sudo find / -type f -size +1M -print 2>/dev/nullProtocol Notes:
- What does
+1Mmean? - Why might
sudobe necessary? - What types of files are typically large?
🧠 Task 3: Identify File Types in /home (Two Methods)
Method A: Using find and file together
ind /home -type f -exec file '{}' \; 2>/dev/nullMethod B: Using a loop
for f in $(find /home -type f 2>/dev/null); do file "$f"; doneProtocol Notes:
- What does the
filecommand do? - Compare the two methods: which is faster or more readable?
- What kinds of file types did you discover?
🧭 Task 4: Locate Executables
Try the following:
which bash
type bash
command -v bashProtocol Notes:
- What are the differences between these commands?
- Which one is POSIX-compliant?
- What does the output tell you?
⚡ Task 5: Use locate and Refresh the Index
- Run:
locate ".txt" - Then refresh the index:
sudo updatedb - Run
locateagain and compare results.
Protocol Notes:
- How fast is
locatecompared tofind? - Did the results change after updating the index?
- What are the limitations of
locate?
🧪 Task 6: Additional Use-Cases
- Use
grep -rl "TODO" ~/to find files containing the word “TODO”. - Try
whereis bashand interpret the output. - If installed, try
fd .txtand compare it tofind.
📁 Task 7: Create Sample Data for Sorting
-
Create a file called
numbers.txtwith the following content (in random order):42 7 19 7 3 42 100 -
Sort the file numerically:
sort -n numbers.txt -
Sort the file in reverse numeric order:
sort -nr numbers.txt
Protocol Notes:
- What does
-ndo? - What does
-rdo? - How does the output change?
🧮 Task 8: Count Duplicate Entries
Run:
sort numbers.txt | uniq -cProtocol Notes:
- What does the output mean?
- Why is
sortnecessary beforeuniq -c? - What happens if you run
uniq -cwithout sorting?
📑 Task 9: Filter Unique and Duplicate Lines
Try:
sort numbers.txt | uniq -u
sort numbers.txt | uniq -dProtocol Notes:
- What’s the difference between
-uand-d? - What lines are considered duplicates?
- How does sorting affect the result?
📋 Task 10: Sort by Column in a CSV File
-
Create a file called
students.csvwith the following content:Alice,85 Bob,92 Charlie,78 Dana,85 Eve,92 -
Sort by score (second column):
sort -t, -k2 students.csv -
Sort by name (first column):
sort -t, -k1 students.csv
Protocol Notes:
- What does
-t,do? - What does
-k2mean? - How does changing the column affect the sort order?
📈 Task 11: Sort and Rank Names by Frequency
-
Create a file
names.txtwith repeated names:Alice Bob Alice Charlie Bob Bob Dana -
Run:
sort names.txt | uniq -c | sort -nr
Protocol Notes:
- What does each part of the pipeline do?
- What does the final output represent?
- How could this be used in real-world scenarios?
📑 Protocol (What to Hand In)
Each student must create a protocol (log) containing:
- Command executed
- Output received (full or partial if very long)
- Explanation (1–2 sentences) of what the command did
- Observation/interpretation of what they learned
Example:
Command: file sample.zip
Output: sample.zip: Zip archive data, at least v2.0 to extract
Explanation: Identified sample.zip as a compressed archive file.
Observation: Shows that zip files are binary and need archive tools to inspect contents.
Command: sudo tail -f /var/log/syslog
Output: (live log output appears)
Explanation: Monitors new log entries in real-time.
Observation: Useful for watching system activity as it happens.