JohnnyOh JohnnyOh - 1 year ago 106
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 ( and running the following command would return the expected filenames for each subdirectory:

find -type d -execdir ./ {} +

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 Source

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/ {
  /^[0-9]/ {
    if (!biggestfilesizeindir[directoryname] || 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download