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.
#!/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
doneLook 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.
mkdir -p proj/src
echo a > proj/src/open.sh
chmod 777 proj/src/open.sh
bash audit.sh proj
bash audit.sh --fix projThe 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.