Linux Exercise 4 - Finding Files and Sorting Data
🧪 Linux File-Finding and Data-Sorting Exercise
🔍 Task 1: Find All Files in /home
Without Errors
Run:
find /home -type f -print 2>/dev/null
Protocol Notes:
- What does
2>/dev/null
do? - 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/null
Protocol Notes:
- What does
+1M
mean? - Why might
sudo
be 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/null
Method B: Using a loop
for f in $(find /home -type f 2>/dev/null); do file "$f"; done
Protocol Notes:
- What does the
file
command 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 bashtype bashcommand -v bash
Protocol 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:
Terminal window locate ".txt" - Then refresh the index:
Terminal window sudo updatedb - Run
locate
again and compare results.
Protocol Notes:
- How fast is
locate
compared 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 bash
and interpret the output. - If installed, try
fd .txt
and compare it tofind
.
📁 Task 7: Create Sample Data for Sorting
-
Create a file called
numbers.txt
with the following content (in random order):427197342100 -
Sort the file numerically:
Terminal window sort -n numbers.txt -
Sort the file in reverse numeric order:
Terminal window sort -nr numbers.txt
Protocol Notes:
- What does
-n
do? - What does
-r
do? - How does the output change?
🧮 Task 8: Count Duplicate Entries
Run:
sort numbers.txt | uniq -c
Protocol Notes:
- What does the output mean?
- Why is
sort
necessary beforeuniq -c
? - What happens if you run
uniq -c
without sorting?
📑 Task 9: Filter Unique and Duplicate Lines
Try:
sort numbers.txt | uniq -usort numbers.txt | uniq -d
Protocol Notes:
- What’s the difference between
-u
and-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.csv
with the following content:Alice,85Bob,92Charlie,78Dana,85Eve,92 -
Sort by score (second column):
Terminal window sort -t, -k2 students.csv -
Sort by name (first column):
Terminal window sort -t, -k1 students.csv
Protocol Notes:
- What does
-t,
do? - What does
-k2
mean? - How does changing the column affect the sort order?
📈 Task 11: Sort and Rank Names by Frequency
-
Create a file
names.txt
with repeated names:AliceBobAliceCharlieBobBobDana -
Run:
Terminal window 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.zipOutput: sample.zip: Zip archive data, at least v2.0 to extractExplanation: 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/syslogOutput: (live log output appears)Explanation: Monitors new log entries in real-time.Observation: Useful for watching system activity as it happens.