295

Looking for a series of commands that will show me the largest files on a drive.

7
  • Would something graphical be fine?
    – RolandiXor
    Apr 20, 2011 at 14:04
  • 7
    nope, running on command line over ssh. Apr 20, 2011 at 14:09
  • What's odd is I have two servers that are running the same thing. One is at 50% disk usage and the other is 99%. I can't find what's causing this. Apr 20, 2011 at 14:17
  • So I'm confused, it says 98% used with du but when I run the gt5 app I get: grab.by/9Vv2 Apr 20, 2011 at 14:43
  • 1
    get size of folders: askubuntu.com/questions/1376125/… du -chd1 . Sep 23, 2022 at 20:32

13 Answers 13

401

If you just need to find large files, you can use find with the -size option. The next command will list all files larger than 10MiB (not to be confused with 10MB):

find / -size +10M -ls

If you want to find files between a certain size, you can combine it with a "size lower than" search. The next command find files between 10MiB and 12MiB:

find / -size +10M -size -12M -ls

apt-cache search 'disk usage' lists some programs available for disk usage analysis. One application that looks very promising is gt5.

From the package description:

Years have passed and disks have become larger and larger, but even on this incredibly huge harddisk era, the space seems to disappear over time. This small and effective programs provides more convenient listing than the default du(1). It displays what has happened since last run and displays dir size and the total percentage. It is possible to navigate and ascend to directories by using cursor-keys with text based browser (links, elinks, lynx etc.)

Screenshot of gt5

On the "related packages" section of gt5, I found ncdu. From its package description:

Ncdu is a ncurses-based du viewer. It provides a fast and easy-to-use interface through famous du utility. It allows to browse through the directories and show percentages of disk usage with ncurses library.

Screenshot of ncdu

7
  • 7
    ncdu is very quick and just what i needed, thanks! I've tried gt5 too, but just canceled it because it was "thinking" too long w/o any feedback Mar 22, 2017 at 21:28
  • 18
    Holy crap, ncdu is amazing, thank you for sharing your findings!
    – While-E
    Sep 20, 2017 at 16:38
  • 3
    I would love if ncdu was pointed out stronger. I need it once in a while and I can't remember the name. Sep 12, 2018 at 12:53
  • Enough of remembering commands thanks to ncdu :)
    – Mr Coder
    Feb 12, 2019 at 6:58
  • 2
    @matt Nope, the output format for the -ls output is hardcoded (see the source code for pred_fls and list_file functions). You could try to approximate the output using the -printf option, post-process the output with awk, or use something like find ... -type f -exec ls -ldh {} \; | column -t
    – Lekensteyn
    Sep 1, 2019 at 15:35
78

My favorite solution uses a mix from several of these good answers.

du -aBM 2>/dev/null | sort -nr | head -n 50 | more

du arguments:

  • -a for "all" files and directories. Leave it off for just directories
  • -BM to output the sizes in megabyte (M) block sizes (B)
  • 2>/dev/null - exclude "permission denied" error messages (thanks @Oli)

sort arguments:

  • -n for "numeric"
  • -r for "reverse" (biggest to smallest)

head arguments:

  • -n 50 for the just top 50 results.
  • Leave off more if using a smaller number

Note: Prefix with sudo to include directories that your account does not have permission to access.

Example showing top 10 biggest files and directories in /var (including grand total).

cd /var
sudo du -aBM 2>/dev/null | sort -nr | head -n 10
7555M   .
6794M   ./lib
5902M   ./lib/mysql
3987M   ./lib/mysql/my_database_dir
1825M   ./lib/mysql/my_database_dir/a_big_table.ibd
997M    ./lib/mysql/my_database_dir/another_big_table.ibd
657M    ./log
629M    ./log/apache2
587M    ./log/apache2/ssl_access.log
273M    ./cache
1
  • 2
    Tanx for this Enterprise solution. I just want to complete this by adding 'cd /' before running the command : 'cd /;sudo du -aBM 2>/dev/null | sort -nr | head -n 10 > sizelog.txt'
    – Eyni Kave
    Jan 24, 2022 at 19:25
39

I just use a combination of du and sort.

sudo du -sx /* 2>/dev/null | sort -n

0   /cdrom
0   /initrd.img
0   /lib64
0   /proc
0   /sys
0   /vmlinuz
4   /lost+found
4   /mnt
4   /nonexistent
4   /selinux
8   /export
36  /media
56  /scratchbox
200 /srv
804 /dev
4884    /root
8052    /bin
8600    /tmp
9136    /sbin
11888   /lib32
23100   /etc
66480   /boot
501072  /web
514516  /lib
984492  /opt
3503984 /var
7956192 /usr
74235656    /home

Then it's a case of rinse and repeat. Target the subdirectories you think are too big, run the command for them and you find out what's causing the problem.

Note: I use du's -x flag to keep things limited to one filesystem (I have quite a complicated arrangement of cross-mounted things between SSD and RAID5).

Note 2: 2>/dev/null redirects any error messages into oblivion. If they don't bother you, it's not obligatory.

1
  • 1
    When I run this command du descends into child directories. From the du man page: "Summarize disk usage of each FILE, recursively for directories."
    – Jamie
    Apr 27, 2016 at 16:14
18

To display the biggest top-20 directories (recursively) in the current folder, use the following one-liner:

du -ah . | sort -rh | head -20

or (more Unix oriented):

du -a . | sort -rn | head -20

For the top-20 biggest files in the current directory (recursively):

ls -1Rs | sed -e "s/^ *//" | grep "^[0-9]" | sort -nr | head -n20

or with human readable sizes:

ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20

Please note that -h is available for GNU sort only, so to make it work on OSX/BSD properly, you've to install it from coreutils. Then add its folder into your PATH.

So these aliases are useful to have in your rc files (every time when you need it):

alias big='du -ah . | sort -rh | head -20'
alias big-files='ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20'
10

qbi's answer is correct but it will be very slow when there are a lot of files since it will start a new ls process for each item.

a much faster version using find without spawning child processes would be to use printf to print the size in bytes (%s) and the path (%p)

find "$directory" -type f -printf "%s - %p\n" | sort -n | tail -n $num_entries

1
  • 1
    Confirm that this is much faster
    – Cookie
    Sep 8, 2014 at 16:45
10

A great, user-friendly tool referenced in the answer to a similar question is NCurses Disk Usage tool:

sudo ncdu /
1
  • Well, starting at the root of the filesystem was exactly the opposite of pointless to me. It allowed me to identify the most important places to target for space saving across the whole filesystem. Regardless, you can use the mentioned to start at any directory. Check out the docs for that tool. But, as requested, here is the link to the source answer.
    – janoside
    Sep 11, 2019 at 0:52
9

This seems like the perfect application for find:

find $DIRECTORY -type f -exec ls -s {} \; | sort -n | tail -n 5

This command will find all files in directory $DIRECTORY and execute ls -s on them. The last command prints the allocated size of a file plus the filename. The result is sorted numerically and and the last five entries are shown. So as result you'll see the largest 5 files in $DIRETORY or any subdirectory. If you enter tail -n 1 you'll see only the largest file.

Furthermore you can play around a lot with find. For instance you can look for files which are younger than n days (-ctime -n) or which belong to special users (-user johndoe).

6

When I need make more free space on servers I use this command. It find all files bigger then 50 MB and "du -h" make better list of files and "sort -n" after pipe make list numericcaly sorted by file size.

find / -size +50M -type f -exec du -h {} \; | sort -n
2

Try Baobab, it gives you a graphical overview of files and folders, you can see where the real space hogs are and delete them with one click https://help.ubuntu.com/community/Baobab

2
  • 2
    In this particular question, the OP prefers a command line method. See the comments to the question. I'll edit the question as well.
    – user25656
    Jan 29, 2013 at 12:10
  • Amazing utility if you have access to GUI Aug 13, 2020 at 16:02
1

To find all GB files for instance I would use du and grep, though the other methods mentioned here seem great as well.

du -h -a /dir | grep "[0-9]G\b"  

You can also get fancy with the --except option that du has.

0

You could also sort files by size:

find . -type f -exec du -h {} \; | sort -k1 -h

It finds only files and executes du -h for every file, which shows the size of the file. Lastly, we sort the output of find/du according to the first column (in human readable format).

The last printed file is the largest one.

0

You can use the command to see the largest files while skipping the directories:

sudo find / -type f -printf “%s\t%p\n” | sort -n | tail -1
find $HOME -type f -printf ‘%s %p\n’ | sort -nr | head -10

To find all files that are larger than 100MiB (this is not 100MB, see here if you are confused):

find / -size +100M -ls

the command below will display the largest 5 files in the folder $DIRECTORY:

find $DIRECTORY -type f -exec ls -s {} \; | sort -n | tail -n 5

USING du : The command below which uses du, display the directories with the 20 largest sizes in the home working folder:

sudo du -a /home | sort -n -r | head -n 20

Now in order now to display the largest directories/files including sub-folders, run:

du -Sh | sort -rh | head -n 10

Using ls:

To list the 5 top largest files in the /bin directory , issue the command below:

ls -lSh /bin | head -5

You can also use the Disk Usage Analyzer or Baobao as shown here for example.

0

I think my answer here is useful: https://unix.stackexchange.com/questions/728364/why-doesnt-df-show-the-filesystem-disk-space-of-the-path-that-i-give-it/728373#728373

but the short summary is TLDR:

For space usage do:
df -h path2inode
above for systemfile usage

du -hs path2inode
du -hs ~ | grep data | head -5
du -hs ~ | sort rh | head -5
above for specific inode. Might be slow. 

To see space your using for specific files & total limit run both commands. 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .