$_ CLI In Depth
Learning progress 0%
Build · Users, Permissions & sudo

Build: A World-Writable File Auditor

Write audit.sh to list world-writable files in a directory, then add a --fix mode that prints chmod commands without running them.

~30 min Beginner 2 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: List world-writable files

The directory /workspace/proj holds files with several modes. Some are world-writable.

Create /workspace/audit.sh. Given a directory argument, it prints the world-writable regular files under that directory, one per line, sorted.

Step 2: Add a fix mode that prints chmod lines

Rewrite /workspace/audit.sh so that bash audit.sh --fix DIR prints one chmod o-w PATH line for each world-writable file, and prints nothing else.

The plain form without --fix must still print the paths. The modes of the files must not change.

Files

  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

walkthrough.sh

BASH36 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: List world-writable files
# (the lab prepares this first)
# cd /workspace; rm -rf proj; mkdir -p proj/src proj/tmp; echo a > proj/src/main.c; chmod 644 proj/src/main.c; echo b > proj/tmp/scratch.txt; chmod 666 proj/tmp/scratch.txt; echo c > proj/readme.md; chmod 664 proj/readme.md; echo d > proj/src/open.sh; chmod 777 proj/src/open.sh
cd /workspace
cat > audit.sh <<&#039;EOF&#039;
#!/bin/bash
find "$1" -type f -perm -002 | sort
EOF

# Step 2: Add a fix mode that prints chmod lines
# (the lab prepares this first)
# cd /workspace; rm -rf proj; mkdir -p proj/src proj/tmp; echo a > proj/src/main.c; chmod 644 proj/src/main.c; echo b > proj/tmp/scratch.txt; chmod 666 proj/tmp/scratch.txt; echo c > proj/readme.md; chmod 664 proj/readme.md; echo d > proj/src/open.sh; chmod 777 proj/src/open.sh
cd /workspace
cat > audit.sh <<&#039;EOF&#039;
#!/bin/bash
fix=0
if [ "$1" = "--fix" ]; then
  fix=1
  shift
fi
dir=$1
if [ -z "$dir" ] || [ ! -d "$dir" ]; then
  echo "usage: audit.sh [--fix] DIR" >&2
  exit 2
fi
find "$dir" -type f -perm -002 | sort | while IFS= read -r path; do
  if [ "$fix" -eq 1 ]; then
    echo "chmod o-w $path"
  else
    echo "$path"
  fi
done
EOF

Lessons this build uses