Skip to content

Linux Exercise 3 - Exploring File Contents

πŸ“ Linux Exercise: Exploring File Contents

🎯 Objective

Students will practice inspecting files in Linux using commands such as file, head, tail, objdump, od, strings, xxd, less, and cat.
They will create a protocol documenting all commands, outputs, and observations.


πŸ”¨ Task Instructions

Part 1: Identifying File Types

1. Choose several files from your system:

  • A text file (e.g., /etc/hosts)
  • A binary executable (e.g., /bin/ls)
  • A compressed archive (e.g., a .zip file)

2. Create a zip file for analysis (if you don’t already have one):

Terminal window
zip sample.zip /etc/hosts /etc/resolv.conf
  • This creates sample.zip containing two files.

3. Use file to determine the type of each file:

Terminal window
file /etc/hosts
file /bin/ls
file sample.zip

Observation: Note what type each file is and why that matters.


Part 2: Inspecting Text Files

4. Use head and tail:

  • View the first 10 lines of a text file:
Terminal window
head /etc/hosts
  • For tail, use a file that is regularly updated, e.g., the system log:
Terminal window
sudo tail -f /var/log/syslog
  • Observe new log entries as they appear in real-time.

5. Experiment with more lines:

Terminal window
head -n 20 /etc/hosts
tail -n 20 /var/log/syslog

Part 3: Examining Binary Files

6. Use objdump on a binary executable (/bin/ls):

  • Show headers:
Terminal window
objdump -x /bin/ls
  • Disassemble a few instructions:
Terminal window
objdump -d /bin/ls | head -n 20

7. Use od to dump the binary in different formats:

Terminal window
od -c /bin/ls # ASCII characters
od -x /bin/ls # Hexadecimal
od -b /bin/ls # Octal

Part 4: Extracting and Viewing Text from Binaries

8. Use strings to list readable text from the binary:

Terminal window
strings /bin/ls | head -n 20

9. Use xxd to create a hex dump:

Terminal window
xxd /bin/ls | head -n 20

Part 5: Viewing Full Content

10. Use cat and less on a text file:

  • Print the whole file:
Terminal window
cat /etc/hosts
  • Scroll interactively:
Terminal window
less /etc/hosts

πŸ“‘ 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.