Skip to Content
Classic Shell Scripting
book

Classic Shell Scripting

by Arnold Robbins, Nelson H. F. Beebe
May 2005
Intermediate to advanced
560 pages
15h 18m
English
O'Reilly Media, Inc.
Content preview from Classic Shell Scripting

Filesystem Space Information

With suitable options, the find and ls commands report file sizes, so with the help of a short awk program, you can report how many bytes your files occupy:

$ find -ls | awk '{Sum += $7} END {printf("Total: %.0f bytes\n", Sum)}'
Total: 23079017 bytes

However, that report underestimates the space used, because files are allocated in fixed-size blocks, and it tells us nothing about the used and available space in the entire filesystem. Two other useful tools provide better solutions: df and du.

The df Command

df (disk free) gives a one-line summary of used and available space on each mounted filesystem. The units are system-dependent blocks on some systems, and kilobytes on others. Most modern implementations support the -k option to force kilobyte units, and the -l (lowercase L) option to include only local filesystems, excluding network-mounted ones. Here is a typical example from one of our web servers:

$ df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda5              5036284   2135488   2644964  45% /
/dev/sda2                38890      8088     28794  22% /boot
/dev/sda3             10080520   6457072   3111380  68% /export
none                    513964         0    513964   0% /dev/shm
/dev/sda8               101089      4421     91449   5% /tmp
/dev/sda9             13432904    269600  12480948   3% /var
/dev/sda6              4032092   1683824   2143444  44% /ww

GNU df provides the -h (human-readable) option to produce a more compact, but possibly more confusing, report:

$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 4.9G 2.1G 2.6G 45% / /dev/sda2 38M 7.9M 29M 22% ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Complete Bash Shell Scripting

Complete Bash Shell Scripting

Narendra Kumar Reddy Polu
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 0596005954Errata Page