Skip to content

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:

Terminal window
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:

Terminal window
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

Terminal window
ind /home -type f -exec file '{}' \; 2>/dev/null

Method B: Using a loop

Terminal window
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:

Terminal window
which bash
type bash
command -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

  1. Run:
    Terminal window
    locate ".txt"
  2. Then refresh the index:
    Terminal window
    sudo updatedb
  3. Run locate again and compare results.

Protocol Notes:

  • How fast is locate compared to find?
  • 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 to find.

📁 Task 7: Create Sample Data for Sorting

  1. Create a file called numbers.txt with the following content (in random order):

    42
    7
    19
    7
    3
    42
    100
  2. Sort the file numerically:

    Terminal window
    sort -n numbers.txt
  3. 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:

Terminal window
sort numbers.txt | uniq -c

Protocol Notes:

  • What does the output mean?
  • Why is sort necessary before uniq -c?
  • What happens if you run uniq -c without sorting?

📑 Task 9: Filter Unique and Duplicate Lines

Try:

Terminal window
sort numbers.txt | uniq -u
sort 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

  1. Create a file called students.csv with the following content:

    Alice,85
    Bob,92
    Charlie,78
    Dana,85
    Eve,92
  2. Sort by score (second column):

    Terminal window
    sort -t, -k2 students.csv
  3. 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

  1. Create a file names.txt with repeated names:

    Alice
    Bob
    Alice
    Charlie
    Bob
    Bob
    Dana
  2. 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.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.
Terminal window
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.