$_ CLI In Depth
Learning progress 0%
build permissions users sudo

Build: A World-Writable File Auditor

5 min read Module 5: Users, Permissions & sudo Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:13

The job

A world-writable file has the other-write bit set, which is the value 2 in the last octal digit of its mode. Any account on the system may change such a file, regardless of its owner or group. Some are intentional, such as a shared scratch area, and most are mistakes. An auditor finds them so that a person can decide which ones are wrong.

The auditor you build accepts a directory argument and prints one path per line for each regular file under that directory whose mode includes the other-write bit. It should print nothing for a clean tree, and it should exit with a clear status when the argument is missing or is not a directory. The second version adds a fix mode that prints the chmod command for each file, so a reviewer can read the change before anything is modified.

Requirements

  • Accept one directory argument, and print matching paths relative to that argument, one per line.
  • Match regular files only, so directories with the write bit are not reported.
  • Print the paths in sorted order, so that two runs give identical output.
  • With the --fix option, print one chmod o-w command per file, and do not change any mode.
  • Exit with status 2 and a usage message when the directory is missing.

Finding the other-write bit

The find command can match on mode. The expression -perm -002 matches files where the other-write bit is set, and the minus sign means at least this bit, so the owner and group bits do not matter. Adding -type f restricts the match to regular files. The output is not guaranteed to be in any particular order, so pipe it through sort before printing.

Use the directory argument as the starting point of the search, and print paths exactly as find returns them. That way the output is relative to the argument you gave, which makes the results easy to compare in a test.

Reference solution

The script below meets every requirement. It parses the optional flag, checks the directory, runs find, and prints either the path or the chmod line for each match. It fits in one short file that you can keep in a repository.

BASH20 lines
#!/bin/bash
# audit.sh: list world-writable regular files under a directory.
# Usage: audit.sh [--fix] DIR
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

Look at the structure first. The flag is consumed by a shift, so the directory is always in $1 afterward. The find output is piped through sort, and the loop reads each path with IFS= read -r, which keeps spaces and backslashes intact in the names.

Testing it

Build a small tree with a known set of modes, then run the script on it. Expected output is the list of paths you set to world-writable, and nothing for files with a clean mode. The exit status for a missing argument should be 2. Testing the boundary cases is more important than testing the happy path, because a tool that silently prints nothing on a typo gives false comfort.

BASH5 lines
mkdir -p proj/src
echo a > proj/src/open.sh
chmod 777 proj/src/open.sh
bash audit.sh proj
bash audit.sh --fix proj

The first run prints proj/src/open.sh. The second prints the chmod line for the same file, and the mode stays at 777 until you run that command yourself. Check the mode after each run with stat -c %a, to confirm that the auditor never changed it.

Keep the script small and readable. A reviewer should be able to read the whole file in a minute and check each line against the requirements list. Long scripts hide mistakes, and an auditor that hides a mistake is worse than no auditor, because people will trust its silence.

Test the auditor against a tree that contains one known bad file and one known good file, and check that the output contains only the bad file. Then run it against a tree with no bad files, and confirm that the output is empty. Both results matter, because a tool that always prints something is as useless as one that never prints anything.

Design notes

The auditor reports, and it does not repair. That separation is deliberate. A tool that changes modes on its own can break a shared directory that someone meant to leave open, and the printed command gives the reviewer a chance to reject the change. The fix mode also makes the output usable in a review, because the lines can be pasted into a change request.

The script trusts the find expression, and it does not parse the output in any clever way. Paths that contain newlines would confuse the loop, which is an accepted limit for a short tool. For a production audit, you would use find with -print0 and a null-separated loop. The limit is worth stating in the documentation, so a reader knows where the tool stops.

Terminal lab · 2 steps · real Linux in your browser

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.

01
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.
Show hint for step 1
Use find with -type f and -perm -002, and pipe the result through sort.
02
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.
Show hint for step 2
Read the optional flag first, shift it away, then choose between printing the path and printing the chmod line.
Check yourself Question 1 of 4
Concept Check Finding the other-write bit recall

Which find expression matches files whose other-write bit is set?

Recap & Key Takeaways

A world-writable file has the other-write bit set, so the auditor matches with find -perm -002 and -type f.
Sorting the output makes the result repeatable, because find does not promise an order.
The fix mode prints chmod commands for review, and the auditor itself never changes a mode.
Test with a tree whose modes you chose, and check the mode after each run to prove the script did not modify files.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue