List the files in a directory in order of file size with the largest files first.

Posted: November 8, 2017. At: 7:24 PM. This was 2 weeks ago. Post ID: 11520
Page permalink: http://securitronlinux.com/bejiitaswrath/list-the-files-in-a-directory-in-order-of-file-size-with-the-largest-files-first/

There are a few ways on Linux to list the files in a directory with the files in order of file size, but I am going to show the easiest ways to accomplish this task.

Using the ls command.

The ls command is the easy way to list all files in order of file-size.

Here is an example.

ubuntu ~/Documents $ ls -Slhu

That will do the job just fine.

The ncdu utility.

This is another way to list all files according to the filesize.

Install this very useful utility.

sudo apt install ncdu

Then just run it in a directory to see the file listing.

--- /home/ubuntu/Documents ------------------------------------------------------------------------------------------------------------------------------------------------
   14.6GiB [##########]  crackstation.txt
   22.4MiB [          ] /wpscan
   19.6MiB [          ]  system.save
   16.6MiB [          ] /impacket-read-only
    9.2MiB [          ]  SYSTEM
    4.7MiB [          ] /python
  256.0KiB [          ]  SECURITY
  256.0KiB [          ]  SAM
   64.0KiB [          ]  sam.save
   64.0KiB [          ]  secretsdump.py
   32.0KiB [          ]  security.save
   12.0KiB [          ]  read
   12.0KiB [          ]  a.out
    8.0KiB [          ]  virii.S
    8.0KiB [          ]  recipe-502268-1.py
    8.0KiB [          ] /.bundle
e   4.0KiB [          ] /.config
    4.0KiB [          ]  hole.o
    4.0KiB [          ]  id_rsa
    4.0KiB [          ]  read.c
    4.0KiB [          ]  hashes.out
    4.0KiB [          ]  my.c
    4.0KiB [          ]  nikto-pinterest-log.log
    4.0KiB [          ]  id_rsa.pub

The largest file at the top is highlighted. But sadly it is not colorized.

The find command can also list the largest files in a directory. This is a much more complex command, but it does work very well indeed.

ubuntu ~/Documents $ find . -type f -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}
568K    ./impacket-read-only/impacket/system_errors.py
568K    ./impacket-read-only/.svn/pristine/86/86acb8dee60e3d45200741c583ea7046b1ebb924.svn-base
576K    ./impacket-read-only/build/lib.linux-x86_64-2.7/impacket/hresult_errors.py
576K    ./impacket-read-only/impacket/hresult_errors.py
576K    ./impacket-read-only/.svn/pristine/26/26b3d102fb62a22906b5e45781dfecb601686427.svn-base
1.5M    ./wpscan/data.zip
9.3M    ./SYSTEM
19M     ./wpscan/.git/objects/pack/pack-9d321cf60e3e0fedc838fe768822143c3fd91cbe.pack
20M     ./system.save
15G     ./crackstation.txt

Here is another use of the du command, this will list the 10 largest files in a directory.

ubuntu ~/Documents $ du -a -BM | sort -n -r | head -n 20
15043M  .
14969M  ./crackstation.txt
23M     ./wpscan
20M     ./system.save
19M     ./wpscan/.git/objects/pack/pack-9d321cf60e3e0fedc838fe768822143c3fd91cbe.pack
19M     ./wpscan/.git/objects/pack
19M     ./wpscan/.git/objects
19M     ./wpscan/.git
17M     ./impacket-read-only
10M     ./SYSTEM
7M      ./impacket-read-only/.svn/pristine
7M      ./impacket-read-only/.svn
5M      ./python/pycrypto-2.6
5M      ./python
5M      ./impacket-read-only/impacket
5M      ./impacket-read-only/build/lib.linux-x86_64-2.7/impacket
5M      ./impacket-read-only/build/lib.linux-x86_64-2.7
5M      ./impacket-read-only/build
2M      ./wpscan/spec/samples
2M      ./wpscan/spec

These commands are the best way to find space hogging files in a certain directory. Very good to know if the hard drive is running out of space.

No comments have been made. Use this form to start the conversation :)

Leave a Reply