A practical example of the du and awk commands

Español English

GNU / Linux

Today I bring you an interesting tip about du command, the problem was proposed by a friend in the following way: I want to know the available hard disk space, I told him: use the df command.

$ df-h

My friend replied, and if I want to know what directories are occupying more space on the hard disk, use du I said:

$ du -sh DIR/*

for the case:

$ sudo du -sh /*

And if I want to exclude one partition, asked again, use the option --exclude

$ du -sh --exclude=partion-mount-point /*

For example we can exclude home, proc, run

$ sudo du -sh --exclude=/home --exclude=/proc --exclude=/run /*

Not very happy my friend returned to the attack :), that partially solves the problem because if I have many partitions and servers would be tedious to type again and again the --exclude option, suspecting that this might not be the last question :) I said: we should find a tool that does what you really want, but then I thought: this can be solved with a script, so here the solution.

1. Read the fstab file

2. Find the file fstab entries that refer to real partitions (excluding swap, bind,…)

3. Generate txt file in /tmp with actual partitions of the fstab entries

4. Use the -X option to read from a file what entries should be excluded.

$ awk '$1 !~ /(^#)|^$/ && $3 !~ /(none|swap)/ && $2 != "/" {print $2} END {print "/proc\n/run\n/var/run\n/dev"}' /etc/fstab > /tmp/du-exclude.txt && sudo du -sh -X /tmp/du-exclude.txt /*

We explain the previous statement from the following fstab file:

# /etc/fstab
# Created by anaconda on Thu Aug 25 11:37:07 2016
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/lcmobile_group-root 	/                       ext4    defaults        1 1
/dev/mapper/lcmobile_group-home 	/home                   ext4    defaults,acl    1 2
/dev/mapper/lcmobile_group-swap 	swap                    swap    defaults        0 0
/home/sedlav/www 		        /var/www		        none 	bind

1. AWK reads the fstab file line by line.

2. The first condition:$1 !~ /(^#)|^$/ ignores empty lines or comments (lines that start with #).

3. The second condition$3 !~ /(none|swap)/ ignores the lines that contain none or swap in the 3rd column.

4. The third condition$2 != "/"includes all lines where the second column is different from /.

5. AWK print the second column for the all lines that meet all previous conditions.

6. AWK add /proc /run /var/run /dev lines to du-exclude.txt file

7. du excludes all dirs found in du-exclude.txt

If we execute the above script the du-exclude.txt file would be:


Further readings

  • man du
  • man awk

2 thoughts on “A practical example of the du and awk commands”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.