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

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers UNIX commands to display the biggest file by size in a directory
# 1  
Old 12-04-2017
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
Code:
[/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?
# 2  
Old 12-04-2017
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.
# 3  
Old 12-04-2017
Hi.

GNU sort has an option for this. Here's an example:
Code:
#!/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:
Code:
$ ./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
# 4  
Old 12-04-2017
You might want to give this version a try:
Code:
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.
# 5  
Old 12-05-2017
Thanks i tried this
Code:
du -ah "pathname" | sort -n -r | head -n 50

It helped very much.
# 6  
Old 12-05-2017
This will definitely NOT sort correctly by file sizes!
# 7  
Old 12-05-2017
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

Last edited by rbatte1; 12-05-2017 at 10:54 AM.. Reason: Corrected hicksd8 to RudiC
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Ls directory size reporting byte size instead of file count

I have been searching both on Unix.com and Google and have not been able to find the answer to my question. I think it is partly because I can't come up with the right search terms. Recently, my virtual server switched storage devices and I think the problem may be related to that change.... (2 Replies)
Discussion started by: jmgibby
2 Replies

2. Shell Programming and Scripting

Display the Filename and Size of a File

Hello, all! Working in a Bourne shell. What command would list the filename and size of a file if the size of the file had to be bigger than $a and smaller than $b? Output (if $a is 10 bytes and $b is 50 bytes):test1.txt 15 test2.txt 30 test3.txt 50 Thanks, Ann :p (3 Replies)
Discussion started by: LowlyIntern
3 Replies

3. UNIX for Dummies Questions & Answers

directory size in unix

hi everyone my q is that i want to know what 's the reason behind the direcotry size in unix like if i do ls -ltr in my home directory it will list all the files and directory .. then the size of directory is either 512 or 1024 .. and every directory is containing files in... (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

4. Shell Programming and Scripting

How to display file name and its size ?

Suppose there are 3 files inside a directory, then i need to use display command which will display, the file name and its size. Is it possible? The output should llook like this only file1 1248 file2 3024 file3 3056 (6 Replies)
Discussion started by: sundaresanv
6 Replies

5. Solaris

Directory size larger than file system size?

Hi, We currently have an Oracle database running and it is creating lots of processes in the /proc directory that are 1000M in size. The size of the /proc directory is now reading 26T. How can this be if the root file system is only 13GB? I have seen this before we an Oracle temp file... (6 Replies)
Discussion started by: sparcman
6 Replies

6. UNIX for Dummies Questions & Answers

Display directory size

Hi all, Is there any built in function that can display the content of a directory showing the size of directories? I want to see the content of a directory without recursion. I don't want to see the content of all subdirectories. I want to see the contained files with their size and the... (6 Replies)
Discussion started by: chebarbudo
6 Replies

7. UNIX for Dummies Questions & Answers

Disk Usage in GB and Unix command to find the biggest file/folder

Hi All, Please help me out 1) Command to find the disk usage in GB. I know that du -k will give in kilobites. 2) How to find the Biggest file/folder in a given set of files/folders. Thanks in advance Regards, Manas (8 Replies)
Discussion started by: manas6
8 Replies

8. UNIX for Advanced & Expert Users

Unix Commands to display the Filesnames

Query: In a directory there are 20 files, I have to display the Filenames which contains the content as JAVA in any line of the file. Please tell me the command or commands. (3 Replies)
Discussion started by: kish_kk84
3 Replies

9. UNIX for Dummies Questions & Answers

Display Directory Size - DF?

How can I display the size of a directory and contents witin a directory df only gives me the mounts (3 Replies)
Discussion started by: t4st33@mac.com
3 Replies

10. UNIX for Dummies Questions & Answers

How can I get the directory size in Unix?

Hi,everyone, Can I use UNIX command to get a directory size in UNIX ? I am developing a software using C in UNIX. How can I get the directory size? Thanks Harry (1 Reply)
Discussion started by: HarryHong
1 Replies
Login or Register to Ask a Question