skip to Main Content

How to find the largest as in total size (by length) header file (.h) in the /usr directory ?
To search in subdirectories too.
I did this as follows:

find . - name *.h | xargs awk | sort -n | less .

But this only outputs all files with the extension .h and their dimensions

2

Answers


  1. Something like

     find /usr -name "*.h" -printf "%st%pn" | awk '$1 > maxsize { maxsize = $1; maxline = $0 } END { print maxline }'
    

    (assuming none of the files have newlines in their names, of course)

    or if you have GNU datamash available:

    find /usr -name "*.h" -printf "%st%pn" | datamash -f max 1 | cut -f1,2
    

    With GNU find, the -printf command lets you print lots of information about a file, including its size in bytes (%s) and its name (%p). Finding the max from that is easy.

    Login or Signup to reply.
  2. With GNU tools:

    find /usr -type f -name "*.h" -printf "%s %p" | sort -zrnk1,1 | head -zn1 | tr '' 'n'
    

    This allows filenames with newlines, find outputs a NUL terminated string and the following commands use NUL as delimiter instead of a newline. At the end of the pipeline, tr converts the delimiter back to a newline.

    If you only want the filename, add cut to remove the first space separated field:

    find /usr -type f -name "*.h" -printf "%s %p" | sort -zrnk1,1 | head -zn1 | 
      tr '' 'n' | cut -d' ' -f2-
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search