Finding large files and listing file/directory size on Linux

When you log onto (SSH) onto a machine, you may be greeted with something like this

Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-34-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
  
  System information as of Mon Aug 14 23:14:42 UTC 2017
  
  System load:  0.0                Processes:           117
  Usage of /:   60.3% of 19.55GB   Users logged in:     1
  Memory usage: 34%                IP address for eth0: 172.32.40.45
  Swap usage:   4%

Notice the Usage of /: 60.3% of 19.55GB

There's one way to tell how much disk space you have left. What are some others?

Find all the disks and their file sizes

There's the classic df -h as shown below

ubuntu@ip-172-32-40-45:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            492M   12K  492M   1% /dev
tmpfs           100M  364K   99M   1% /run
/dev/xvda1       20G   12G  6.9G  64% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none            497M     0  497M   0% /run/shm
none            100M     0  100M   0% /run/user

Okay, so above it shows I have 20G on the root directory / and I am using 12G with 6.9G left.

What if I want to track down the large files?

You could use find / -xdev -type f -size +100M to start finding all of the files that are bigger than 100 MB.

Note that you'll probably get a lot of permission denied errors if you don't run it with sudo privileges.

ubuntu@ip-172-32-40-45:~$ sudo find / -xdev -type f -size +100M
/home/ubuntu/.forever/6n8K.log
/home/ubuntu/.forever/9T0G.log
/home/ubuntu/.forever/UP6l.log
/home/ubuntu/.forever/pcheck1.log
/home/ubuntu/.forever/pcheck3.log
/home/ubuntu/.forever/dYDy.log
/home/ubuntu/.forever/pcheck4.log
/home/ubuntu/.forever/wDxW.log
/home/ubuntu/.forever/f9UA.log
/home/ubuntu/.forever/iioL.log
/home/ubuntu/.forever/kJQF.log
/home/ubuntu/.forever/xX0D.log
/home/ubuntu/.forever/xeiq.log
/home/ubuntu/.forever/3wFd.log
/swapfile
ubuntu@ip-172-32-40-45:~$

Above it shows a bunch of log files and a /swapfile, so those are my large files.

List how big each directory (and its contents) is

Here's a common issue, say I'm in a directory and I want to know how big everything is within it. I can do ls -lahS to get everything sorted by file size, but you'll see there's a problem with that in the example below:

ubuntu@ip-172-32-40-45:~/apps$ ls -lahS
total 6.8G
-rw-rw-r--  1 ubuntu ubuntu 2.2G Apr 20  2016 9T0G.log
-rw-rw-r--  1 ubuntu ubuntu 1.8G May 20  2016 xX0D.log
-rw-rw-r--  1 ubuntu ubuntu 877M Feb 26  2016 pcheck1.log
-rw-rw-r--  1 ubuntu ubuntu 719M Jan 31  2016 f9UA.log
-rw-rw-r--  1 ubuntu ubuntu 564M Feb 26  2016 pcheck3.log
-rw-rw-r--  1 ubuntu ubuntu 404M Mar  5  2015 UP6l.log
-rw-rw-r--  1 ubuntu ubuntu 347M Jan 31  2016 kJQF.log
-rw-rw-r--  1 ubuntu ubuntu 324M Mar  3  2016 dYDy.log
-rw-rw-r--  1 ubuntu ubuntu 295M Feb 26  2016 wDxW.log
drwxrwxr-x  6 ubuntu ubuntu 4.0K Mar  7  2015 cutecrawler
drwxrwxr-x  2 ubuntu ubuntu 4.0K Apr  9  2015 grafana
drwxrwxr-x  2 ubuntu ubuntu 4.0K Apr  9  2015 influxdb
drwxrwxr-x  4 ubuntu ubuntu 4.0K Jun 18  2015 catwine
drwxrwxr-x  9 ubuntu ubuntu 4.0K Oct  1  2014 priceweb
drwxrwxr-x  5 ubuntu ubuntu 4.0K May 20  2016 supertracker
drwxrwxr-x  3 ubuntu ubuntu 4.0K Mar  5  2015 friends

The lines at the bottom are directories, and it says they're 4.0K. ls doesn't actually tell you how big the files are in those directories.

Instead, you'll want to use du -sch * to determine how big all those directories are.

ubuntu@ip-172-32-40-45:~/apps$ du -sch *
38M     cutecrawler
20M     grafana
16M     influxdb
47M     catwine
48M     priceweb
20M     supertracker
224K    friends
187M    total

If you want to look at a different directory, just replace the * with whichever directory you want to investigate, as in the example below:

ubuntu@ip-172-32-40-45:~/apps$ sudo du -sch /var/*
1.8M    /var/backups
118M    /var/cache
20K     /var/crash
154M    /var/lib
4.0K    /var/local
0       /var/lock
11M     /var/log
4.0K    /var/mail
4.0K    /var/opt
0       /var/run
32K     /var/spool
4.0K    /var/tmp
16K     /var/www
283M    total

Hopefully these three Linux tools will help you track down large files and help monitor your disk space usage.