I'd like to rewrite these 2 commands so they will use only POSIX-compliant switches:

find "$TARGET_DIR" -maxdepth 1 -type d -printf '(DIR)  %f\n'
find "$TARGET_DIR" -maxdepth 1 -type f -printf '%s  %f  ' -exec file -b {} \;

-maxdepth 1 can probably be replaced with -prune, but -printf will require a more complicated redirection.

  • printf '(DIR) %s\n' "$TARGET_DIR/"*/ should work for the first, I think, but just to be sure - what does %f do (I forget?). – mikeserv Feb 23 '15 at 2:17
  • FYI, -prune equates to -maxdepth 0 not -maxdepth 1. Related: Limit POSIX find to specific depth? – Wildcard May 22 '16 at 7:44


find "$TARGET_DIR//." \( -name . -o -prune \) -type d -exec sh -c '
  for f do
    printf "(DIR) %s\n" "${f:-/}"
  done' sh {} +

It would be simpler for the equivalent of -mindepth 1 -maxdepth 1:

find "$TARGET_DIR//." \( -name . -o -prune \) -type d -exec sh -c '
  for f do
    printf "(DIR) %s\n" "${f##*/}"
  done' sh {} +

For the second one:

find "$TARGET_DIR//." ! -name . -prune -type f -exec sh -c '
  for f do
    size=$(($(wc -c < "$f") +0)) || continue
    printf %s "$size ${f##*/} "
    file -b -- "$f"
  done' sh {} +
  • wouldn't be better to read the size with stat? – eadmaster Mar 30 '17 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.