Search This Blog

2012-11-30

Disk Usage in *nix

This page will be handful of useful commands you can use to find disk statistics. I will start off with  some  simple ones and then later when I feel the need or get time will provide further ones.

Total Disk Usage

One of my favourite disk usage commands to find total disk usage on only mounted partitions is:
df -cl
-c - display the grand total
-l - only local partitions

I will be providing much more interesting  command and information at a later point in time. Including visual space usage using some bash and shell scripts as well as the differences between df and du when it comes to tracking used disk space.

Note: df -c does not work in linux. I have not yet found a way to print out total local disk usage for this yet.

DF Not Updating Actual Disk Space

Sometimes when you delete a file it will not show the free space using the 'df' command. The reason being is that the file may still be held open by a process in which the file descriptor handles still point to the file.

To find these processes run the following commands:
lsof | grep 'deleted'
ls -ld /proc/* | grep '(deleted)'

The solution to updating the space shown by df is to kill the process so
kill $PID
kill -9 $PID

Alternatively I believe the 'du' does not experience the same issues when looking for free disk space.
du -h

References: http://mattiasgeniar.be/2010/11/26/df-command-in-linux-not-updating-actual-diskspace-wrong-data/

Swap Usage

Something I consistently forget when it comes to disk usage is swap space usage. It does use up a significant amount of space. Especially if you're planning disk capacity on a large scale.

In order to get swap usage in freebsd you can run the swapinfo command:
swapinfo
Device          1K-blocks     Used    Avail Capacity
/dev/da0s1b       4194304      204  4194100     0%
It gives you the total used and available.
Alternatively you could you vmstat to monitor the usage over a period of time or use the top command as well.

In linux it's a bit different. You can either run "free" or "swapon" commands as well as "vmstat" or "top"

free
             total       used       free     shared    buffers     cached
Mem:      32949016    3126216   29822800          0     373764    1755100
-/+ buffers/cache:     997352   31951664
Swap:     16777208          0   16777208

swapon -s
Filename                                Type            Size    Used    Priority
/dev/mapper/VolGrp-Vol2                 partition       16777208        0       -1

vmstat -an
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
 6  0      0 29824876 1846148 1077156    0    0     2   146  171  156  3  1 96  0  0

top -b | grep Swap
Swap: 16777208k total,        0k used, 16777208k free,  1755100k cached


Skip Directories in du

The only way to figure out disk utilization in du and skip directories you do not want parsed is to give du a list of directories that you DO want parsed

find . -mindepth 1 -maxdepth 1 -type d -print > .dudirs

The above will list all immediate directories in your local directory and then you can modify them as you please in a text editor before piping them into du

du -sch $(cat .dudirs)


Alternatively, do it in one big command
du -sch $(find . -mindepth 1 -maxdepth 1 -type d -print | grep -v amd_mnt) 

Top Level Disk Usage Summaries

Using a wildcard in the current working directory you can get a summary of all of the directories below the current one you are in.

For example, I am in the directory /volume1, and if I want to know how large each sub-directory is with a grand total I can do the following

du -sch *
73.7M @appstore
4.0K @autoupdate
78.8M @database
24.0K @eaDir
9.3M @smbd.core
12.0K @spool
86.3M @tmp
8.5M @transmissiond.core
139.5G TimeMachine
12.0K aquota.group
12.0K aquota.user
245.8G backup
112.7G homes
43.8G media
19.5G software
561.7G total

As you can see, I get a summary of all the subdirectories as well as a grand total. This is very useful in figuring out what main directories are using up the majority of your space.

-s - summary of directories (only print the grand totals of each sub directory)
-c - print out a total of all of the queried subdirectories at the end
-h - human-readable (print out all directories in summaries of K,M,G instead of just printing in bytes) - might want to remove if you want to sort these properly

Additionally, you can ad a --max-depth=$number to this command to only go a certain depth into the subdirectories or even just specify specific directories in which you want to query like
du /volume1/homes /volume1/media

No comments:

Post a Comment