JohnnyOh JohnnyOh - 7 months ago 17
Bash Question

finding largest file for each directory

I am currently stuck with listing the largest file of each subdirectory in a specific directory.
I succeeded in listing the largest file in a directory by entering the following command (in Debian):

find . -type f -printf "%p\n" | ls -rS |tail -1


I expected entering the command in a shell-file (searchHelper.sh) and running the following command would return the expected filenames for each subdirectory:

find -type d -execdir ./searchHelper.sh {} +


Unfortunately it does not return the largest file for each subdirectory, but something else.

May I get a hint for getting the filename (with absolute path) of the largest file of each subdirectory?

Many thanks in advance

Answer

Give a try to this safe and tested version:

find "$(pwd)" -depth -type f -printf "d%h\0%s %p\0" | awk -v RS="\0" '
  /^d/ {
    directoryname=substr($0,2);
  }
  /^[0-9]/ {
    if (!biggestfilesizeindir[directoryname] || biggestfilesizeindir[directoryname] < $1) {
      biggestfilesizeindir[directoryname]=$1;
      biggestfilesizefilenameindir[directoryname]=substr($0,index($0," ")+1);
    }
  }
  END {
    for (directoryname in biggestfilesizefilenameindir) {
      print biggestfilesizefilenameindir[directoryname];
    }
  }'

This is safe even if the names contain special chars: ' " \n etc.

Comments