#!/bin/sh # # Format the output from "ls -lR" # usage() { echo "usage: $(basename $0) [-h hostname -p path] [-m match] [-d] [-s]" >&2 exit 1 } while getopts ":h:p:m:ds" c do case "$c" in h) hostname=$OPTARG;; p) path=$OPTARG;; m) matchp=$OPTARG;; d) pdate=1;; s) psum=1;; :|'?') usage;; esac done if [ $OPTIND -le $# ]; then usage; fi if [ -n "$hostname" -a -z "$path" -o -n "$path" -a -z "$hostname" ]; then usage fi nawk -v hostname=$hostname -v path="$path" -v matchp=$matchp -v pdate=$pdate \ -v psum=$psum ' BEGIN { if (hostname != "") printf "%s:%s... ", hostname, path > "/dev/stderr" sub(/\/$/, "", path); dir = "." for (i = 0; i < 79; i++) hdrline = hdrline "-" } /^-([r-][w-][SsxTt-])+.+[^:]$/ { # File size = $5; date = sprintf("%3s %2s %5s", $6, $7, $8) file = substr($0, index($0, date) + 13) if (file !~ matchp) next if (dir != "") { if (notfirst) printf "\n" if (dir == ".") dir = "" if (hostname != "") { slash = (dir != "") ? "/" : "" printf "%s\n%s:%s/%s%s\n%s\n", hdrline, hostname, path, dir, \ slash, hdrline } else printf "%s\n/%s\n%s\n", hdrline, dir, hdrline dir = ""; notfirst = 1 } if (pdate) printf "%-54s %9s %s\n", file, size, date else printf "%-68s %9s\n", file, size nf++ } /:$/ { sub(/:$/, ""); sub(/^\.\//, ""); dir = $0 } # Directory END { printf "\n" if (hostname != "") { printf "done (%d %s)\n", nf, (nf == 1) ? "file" : "files" > \ "/dev/stderr" } else if (psum) printf "%d %s found.\n", nf, (nf == 1) ? "file" : "files" }' .