UNIX commands to display the biggest file by size in a directory

Hello guys,

Please i need to know the biggest files in my directory let's say

[/X2/WORK/LOG]$
>du -h | egrep '[0-9[0-9]M|[0-9]G|[0-9][0-9]G'
 195M   ./TMP
 3.6M   ./TP_DEC2012
 146G   .

But here the result it's giving me the biggest directory in the path.

Actually i want to know the biggest file in 146G .

Can anyone help?

Why not drop the -h option to du in favour of -a (to report all files in a directory) and -b (to report byte sizes) so it outputs exact file sizes that you can compare or sort . man du is your friend.
On top, it might not match your (erroneous) regexes.

Hi.

GNU sort has an option for this. Here's an example:

#!/usr/bin/env bash

# @(#) s1       Demonstrate human numeric sort, GNU sort.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sort

pe
FILE=${1-data1}

# Display input.
head $FILE

pl " Results, general numeric sort:"
cat $FILE | # simulate du
sort -g -k1,1

pl " Results, human numeric sort:"
cat $FILE | # simulate du
sort -h -k1,1 

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.9 (jessie) 
bash GNU bash 4.3.30
sort (GNU coreutils) 8.23

 195M   ./TMP
 3.6M   ./TP_DEC2012
 146G   .

-----
 Results, general numeric sort:
 3.6M   ./TP_DEC2012
 146G   .
 195M   ./TMP

-----
 Results, human numeric sort:
 3.6M   ./TP_DEC2012
 195M   ./TMP
 146G   

See man sort for more information.

Best wishes ... cheers, drl

You might want to give this version a try:

rev=""
if [ $# -gt 0 ]
  then
  if [ $1 = '-r' ]
  then
    rev="r"
#   echo $rev
    shift
  fi
fi
ls -l $* |grep -v "total " |sort +4n$rev -5 +8 |more -e

It lists all files (or star name convention) in increasing size or, with "-r" as the first parm, in decreasing size. Ties are alphabetical by file name.

Thanks i tried this

du -ah "pathname" | sort -n -r | head -n 50

It helped very much.

This will definitely NOT sort correctly by file sizes!

At the extreme, find /path/to/dir -type f -ls | sort -bnk 7 might do the trick, but you have to be sure that there are no groups or user names with spaces in them as that will appear to add extra columns.

Using du -h is for human readable output. Follow the advice of RudiC to get output you don't need to mess about with too much for the machine to understand.

Robin