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.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
eadmaster
  • 1,573
  • 4
  • 18
  • 30
  • `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 02:17
  • FYI, `-prune` equates to `-maxdepth 0` not `-maxdepth 1`. Related: [Limit POSIX find to specific depth?](http://unix.stackexchange.com/q/275637/135943) – Wildcard May 22 '16 at 07:44

1 Answers1

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 {} +
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501