Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

UNIX Disk Usage

  • Print
  • + Share This
This chapter is from the book

A Closer Look with du

The df command is one you'll use often as you get into the groove of system administration work. In fact, some sysadmins have df e-mailed to them every morning from cron so they can keep a close eye on things. Others have it as a command in their .login or .profile configuration file so they see the output every time they connect.

Once you're familiar with how the disks are being utilized in your Unix system, however, it's time to dig a bit deeper into the system and ascertain where the space is going.

Task 3.2: Using du to Ascertain Directory Sizes

The du command shows you disk usage, helpfully enough, and it has a variety of flags that are critical to using this tool effectively.

  1. There won't be a quiz on this, but see if you can figure out what the default output of du is here when I use the command while in my home directory:

    # du
    12     ./.kde/Autostart
    16     ./.kde
    412    ./bin
    36     ./CraigsList
    32     ./DEMO/Src
    196    ./DEMO
    48     ./elance
    16     ./Exchange
    1232   ./Gator/Lists
    4      ./Gator/Old-Stuff/Adverts
    8      ./Gator/Old-Stuff
    1848   ./Gator/Snapshots
    3092   ./Gator
    160    ./IBM/i
    136    ./IBM/images
    10464  ./IBM
    76     ./CBO_MAIL
    52     ./Lynx/WWW/Library/vms
    2792   ./Lynx/WWW/Library/Implementation
    24     ./Lynx/WWW/Library/djgpp
    2872   ./Lynx/WWW/Library
    2880   ./Lynx/WWW
    556    ./Lynx/docs
    184    ./Lynx/intl
    16     ./Lynx/lib
    140    ./Lynx/lynx_help/keystrokes
    360    ./Lynx/lynx_help
    196    ./Lynx/po
    88     ./Lynx/samples
    20     ./Lynx/scripts
    1112   ./Lynx/src/chrtrans
    6848   ./Lynx/src
    192    ./Lynx/test
    13984   ./Lynx
    28484  .

    If you guessed that it's the size of each directory, you're right! Notice that the sizes are cumulative because they sum up the size of all files and directories within a given directory. So the Lynx directory is 13,984 somethings, which includes the subdirectory Lynx/src (6,848), which itself contains Lynx/src/chrtrans (1112).

    The last line is a summary of the entire current directory (.), which has a combined size of 28484.

    And what is that pesky unit of measure? Unfortunately, it's different in different implementations of Unix so I always check the man page before answering this question. Within RHL7.2, the man page for du reveals that the unit of measure isn't specifically stated, frustratingly enough. However, it shows that there's a -k flag that forces the output to 1KB blocks, so a quick check

    # du -k | tail -1
    28484  .

    produces the same number as the preceding, so we can safely conclude that the unit in question is a 1KB block. Therefore, you can see that Lynx takes up 13.6MB of space, and that the entire contents of my home directory consume 27.8MB. A tiny fraction of the 15GB /home partition!

NOTE

Of course, I can recall when I splurged and bought myself a 20MB external hard disk for an early computer. I couldn't imagine that I could even fill it, and it cost more than $200 too! But I'll try not to bore you with the reminiscence of an old-timer, okay?

  1. The recursive listing of subdirectories is useful information, but the higher up you go in the file system, the less helpful that information proves to be. Imagine if you were to type du / and wade through the output:

    # du / | wc -l
      6077

    That's a lot of output!

    Fortunately, one of the most useful flags to du is -s, which summarizes disk usage by only reporting the files and directories that are specified, or . if none are specified:

    # du -s
    28484  .
    # du -s *
    4      badjoke
    4      badjoke.rot13
    412    bin
    4      browse.sh
    4      buckaroo
    76     CBO_MAIL
    36     CraigsList
    196    DEMO
    48     elance
    84     etcpasswd
    16     Exchange
    3092   Gator
    4      getmodemdriver.sh
    4      getstocks.sh
    4      gettermsheet.sh
    0      gif.gif
    10464  IBM
    13984  Lynx

    Note in the latter case that because I used the * wildcard, it matched directories and files in my home directory. When given the name of a file, du dutifully reports the size of that file in 1KB blocks. You can force this behavior with the -a flag if you want.

TIP

Tip - The summary vanishes from the bottom of the du output when I specify directories as parameters, and that's too bad, because it's very helpful. To request a summary at the end, simply specify the -c flag.

  1. While we're looking at the allocation of disk space, don't forget to check the root level, too. The results are interesting:

    # du -s /
    1471202 /

    Oops! We don't want just a one-line summary, but rather all the directories contained at the topmost level of the file system. Oh, and do make sure that you're running these as root, or you'll see all sorts of odd errors. Indeed, even as root the /proc file system will sporadically generate errors as du tries to calculate the size of a fleeting process table entry or similar. You can ignore errors in /proc in any case.

    One more try:

    # du -s /*
    5529     /bin
    3683     /boot
    244      /dev
    4384     /etc
    29808    /home
    1        /initrd
    67107    /lib
    12       /lost+found
    1        /misc
    2        /mnt
    1        /opt
    1        /proc
    1468     /root
    8514     /sbin
    12619    /tmp
    1257652  /usr
    80175    /var
    0        /web

    That's what I seek. Here you can see that the largest directory by a significant margin is /usr, weighing in at 1,257,652KB.

    Rather than calculate sizes, I'm going to use another du flag (-h) to ask for human-readable output:

    # du -sh /*
    5.4M  /bin
    3.6M  /boot
    244k  /dev
    4.3M  /etc
    30M   /home
    1.0k  /initrd
    66M   /lib
    12k   /lost+found
    1.0k  /misc
    2.0k  /mnt
    1.0k  /opt
    1.0k  /proc
    1.5M  /root
    8.4M  /sbin
    13M   /tmp
    1.2G  /usr
    79M   /var
    0     /web

    Much easier. Now you can see that /usr is 1.2GB in size, which is quite a lot!

  2. Let's use du to dig into the /usr directory and see what's so amazingly big, shall we?

    # du -sh /usr/*
    121M  /usr/bin
    4.0k  /usr/dict
    4.0k  /usr/etc
    40k   /usr/games
    30M   /usr/include
    3.6M  /usr/kerberos
    427M  /usr/lib
    2.7M  /usr/libexec
    224k  /usr/local
    16k   /usr/lost+found
    13M   /usr/sbin
    531M  /usr/share
    52k   /usr/src
    0     /usr/tmp
    4.0k  /usr/web
    103M  /usr/X11R6

    It looks to me like /usr/share is responsible for more than half the disk space consumed in /usr, with /usr/bin and /usr/X11R6 the next largest directories.

    You can easily step into /usr/share and run du again to see what's inside, but before we do, it will prove quite useful to take a short break and talk about sort and how it can make the analysis of du output considerably easier.

  3. Before we leave this section to talk about sort, though, let's have a quick peek at du within the Darwin environment:

    # du -sk *
    5888   Desktop
    396760 Documents
    84688  Library
    0      Movies
    0      Music
    31648  Pictures
    0      Public
    32     Sites
    

    Notice that I've specified the -k flag here to force 1KB blocks (similar to df, the default for du is 512-byte blocks). Otherwise, it's identical to Linux.

    The du output on Solaris is reported in 512-byte blocks unless, like Darwin, you force 1KB blocks with the -k flag:

    # du -sk *
    1      bin
    1689   boot
    4      cdrom
    372    dev
    13     devices
    2363   etc
    10     export
    0      home
    8242   kernel
    1      lib
    8      lost+found
    1      mnt
    0      net
    155306 opt
    1771   platform
    245587 proc
    5777   sbin
    32     tmp
    25     TT_DB
    3206   users
    667265 usr
    9268   var
    0      vol
    9      xfn

This section has demonstrated the helpful du command, showing how -a, -s, and -h can be combined to produce a variety of different output. You've also seen how successive du commands can help you zero in on disk space hogs, foreshadowing the diskhogs shell script we'll be developing later in this hour.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020