7

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.

2
  • 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
    Commented Feb 23, 2015 at 2:17
  • FYI, -prune equates to -maxdepth 0 not -maxdepth 1. Related: Limit POSIX find to specific depth?
    – Wildcard
    Commented May 22, 2016 at 7:44

1 Answer 1

1

Try:

find "$TARGET_DIR//." \( -name . -o -prune \) -type d -exec sh -c '
  for f do
    f=${f%//.}
    f=${f%"${f##*[!/]}"}
    f=${f##*/}
    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 {} +
1
  • wouldn't be better to read the size with stat?
    – eadmaster
    Commented Mar 30, 2017 at 12:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .