30 du/df Linux-CMD Beispiele

by

In this post, we will be discuss the command “du” or disk usage. The du command is a handy command to check folder/file sizes in Linux. This command comes with lots of options which when
combining them together can give you a very powerful way to check file/folder size usage of your disk.

Syntax for Linux du command

Basic du command usage Beispiels

 

Beispiel 0x001:

Execute du command with no options. As you can see in this Beispiel, using du without any option will list folders recursively by default the size of each file under the directory you
chose. The file size here is shown in KB by default. It will show the hidden files and the size of the parent directory as an entry, and the child directories as separate entries.
However it will go up to the last directory

 

Beispiel 0x002:

If you dont want to see output in KB’s, use the -h option for a human readable output. Adding the -h option will give you the output in a more readable file size format, with base 1024, it
will show the 1024 KB file as 1M which is more easier to read.

 

Beispiel 0x003:

If you are wishing to get the disk space utilization of the whole directory, without going into details of any sub directories. Use the -s option for a summarized output and eventually
files, you can use the -s option.

Or even one level down than just /var to include sub-directories

 

Beispiel 0x004:

Using the -a option “with caution”. If you want to go as much granular as possible, up to the last file in the chain, including any hidden files, use the -a option. Please note
that you can not use -s and -a in the same command.

 

Beispiel 0x005:

We can even control how many levels we can go down the sub directories using the [ –max-depth=N] option. It basically will view everything as long as it is N or less levels under
the directory you specified.

 

Beispiel 0x006:

Now lets do the same as previous Beispiel, but add the –time option
this will increase your visibility, it will show you a new column with the time last modified shown.

 

Beispiel 0x007:

What if you are not interested in some file patterns, and you wish to exclude them, use the [–exclude=pattern] option
Let’s examine the below directory /root/imagefiles/

Now, say we want to view this directory, but we are not interested in the .log file type, let’s do it one more time with the –exclude option

 

Beispiel 0x008:

We can even exclude files coming from different file systems. Examine the below File system, I am using /home as a file system and /home/rsasoc as a different file system. Suppose I
want to check disk utilization of /home only, and ignore /home/rsasoc file system then use -x option.

Observer that /home/rsasoc is showing as zero, when it is actually 69 GB utilized.

 

Beispiel 0x009:

Combining du with sort, to sort the output based on maximum utilization.
Let’s now try to sort the output of du, so we check the maximum utilization files first.

Where 2> /dev/null will direct any errors to /dev/null , sort -r will sort reversely “bigger size first”.

 

Beispiel 0x010:

I have hidden directories in my folder, how can I list only hidden folders size

Let me explain what .[!.]* is all about. In order to understand this you should know . indicates present directory and .. indicates parent directory. what .[!.] indicates is my folder starts
with . and it will not contain . again after the first dot. Which means we are effectively eliminating .. which is my parent directory. and .[!.]* is all the hidden files/folder in that given
directory.

 

Beispiel 0x011:

Above Beispiel lists only hidden files and directories, how about list all folders/files in that directory.

Beispiel output:

 

Beispiel 0x012:

Some times we want to execute this command in many folders which is tedious job, how about making this as an alias so that the typing takes less time.

alias sg=“du -hs * | grep -E ^[0-9]+G“

The first command will list all the folders which are more than or equal to 1M and less than 1000M and second command will show all the files/folder which are more than or equal to 1G

[root@php-faq]#~$ sg
16G Desktop

 

Beispiel 0x013:

If you want to round the way it counts to the nearest block size, so it acts in a way like df “we will get to this next”, you can use the –block-size option

If you haven’t checked “df” command yet, please give it a look at our df command Beispiels whereas du and df might seem similar in the way they show you your disk utilization, in fact
there are lots of differences.
Mostly, “du” will always count on files, and this will be the building block used to provide the total disk space utilized.In a sense of how many files that actually exist, and how they
sum up to build this utilization. whereas “df” will always count on file system as the measure, and by that it will check the utilization it calculates for each file system mounted on
a certain directory.

Another huge misleading difference is du calculates the sum of separate file sizes, df is designed to count blocks used by file system. in a way that if block size is 1 KB “the default
value”, if for instance a file is like 800 bytes utilizing one block. df will count the blocks and multiplies by 1024 to count, whereas again du will sum the actual size of each file,
and outcomes a much more accurate number. Finally, in a world where all Linux file systems are Journaling, these journals are simply used disk space to keep track of files that are
loaded in memory. this disk space used is not of your actual files but rather for the journal of your loaded files in memory. again df will calculate these [though they are not
actually your files], whereas du will not. So basically in a nut shell and to sum up, if you are willing to get a big picture of how heavily your disk is utilized and ready to ignore
willingly a margin or error, df command will be your tool, it is much faster than du and with much less processing. However if you are willing to be very granular, and in the scope of
directories and the zoomed-in picture and willing to spend more time and CPU processing in return, then surely you will be using du and not df.

 

The Command df “disk file system” is one of the very powerful tools every system admin should know. when mastering this command you should understand lot of information about your local and
remote file system details like mount point, disk utilization, partitions etc. In this post, we will find out some of the very common and useful ways to use “df” to monitor your file system.
The df command read its output from /proc/mounts file and a kernel function statfs(2). This file /proc/mounts give you where block files are mounted and statfs(2) will help you query kernel to
get present disk sizes. The df command will combine these two and print some meaningful information for a Linux user.

 

Basic df command usage Beispiele

 

Beispiel 0x001:

Using the “df” without any options. If you use the df command without any arguments, it will show you something like below. You will get columns of File system, number of blocks in 1K
each, number of used blocks, number of available blocks, used percentage and finally the mount point for each file system. As you will get to know this command better, you will come to know
that it is handier to mix it with some options, we will get to that later. You should know about what is block size in Linux before proceeding.

Before going in to Beispiels, we will try to decode some of the above lines.

 

Beispiel 0x002:

Understanding normal mount mappings

/dev/sdb1 partition is mounted on / whose size is 22G, used is 14Gb and available is 6.8Gb.

 

Beispiel 0x003:

Understanding special mount mappings.

Some times, We have to mount some folders in special partitions which are virtual in nature. This will help in preventing accidental deletion of important data for system to work properly. The
above Beispiel /dev folder which contain device file information and may vary with every reboot is actually mounted with a virtual file system.

 

Beispiel 0x004:

Understanding below line

The /run folder is recent implementation which contain process related information and which should not be deleted by any one until next reboot.

 

Beispiel 5:

If you observe below line, the virtual partition is none and mounted on /run/user. In other words this is tactfully virtual file system with the virtual file system, because /run is using tmpfs
virtual partition.

Beispiel 0x006:

Do you know we can mount remote file systems like Samba, NFS, SSH and FTP? Below are Beispiel lines you may see in df command output.

Mounting FTP locally
Beispiel “df -T” output line for ftp mount/share

Mounting SSH locally
Beispiel “df -T” output line for SSH mount/share

Mounting NFS locally
Beispiel “df -T” output line for NFS mount/share

Mounting Samba locally
Beispiel “df -T” output line for samba mount/share

 

Beispiel 0x007:

Let us start exploring df command with options. The first in the list is -h option. As you can see, using the df command without any options can list you the file system usage, however it
lists the size for
each file system in number of blocks. A better way to look at it, is to print the size in a more human readable format, and this is what the -h option does. As you notice below, the -h
option changed the way
the size column looks like. It is most likely that you will be using the df -h “then combine it with more options” than using just df.

 

Beispiel 0x008:

Using the “df” with -T option. Moving on now, the -T options, which prints the type of each file system, and by that it means the format of each file system. As you can notice below, adding the
-T option, will add a new column which is the file system type.

 

Beispiel 0x009:

Using the “df” with -t option. Notice that Linux is case sensitive, the -t options will only list file system with the type you specified in the arguments. This will not print any other file
systems which are mounted at present.

 

Beispiel 0x010:

Note that you can combine the -t with -h for a human readable view, however, make sure that the -t option is the last
option facing the argument “the file system type”. In other words, it should be “df -ht ext3” and not “df -th ext3”.

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 98M 3.4M 95M 4% /boot/efi
/dev/mmcblk0p1 3.7G 1.8G 1.9G 49% /media/surendra/9016-4EF8

 

Beispiel 0x011:

Using the “df” with -x option. The -x option, excludes any file system with the given type in the arguments, in other words, it will print all file systems, that do not match the file system
type you mentioned. This is very neat when you want to focus on file systems and the
one you are mentioning is not interesting for you. The below Beispiel shows how to view all file system that are not tmpfs type.

As you are getting now, we have to make the -x option facing the “tmpfs” which is the argument. “df -Thx tmpfs” will also work in the same fashion, however “df -xhT tmpfs” will not work.

 

Beispiel 0x012:

Using the “df” with -i option to print inode details. Another way to view the utilization of your system is to check the number of inodes utilized by each file system. A side note is that for
each file system type, file system might become full even if there is still space left, and that is because it reached the maximum number of inodes the inode table can take. This is depending
on the file system type. So in some cases, it is handy to know the number of inodes utilized by each file system.

Note: Now columns represent different stuff. Second column represents total number of Inodes, third used, fourth free inodes and fifth shows % used inodes.

 

Beispiel 0x013:

Using the “df” with a certain directory in the arguments. One final thing to mention about df , is that you can use it on a specific directory. Although it will not show the directory
utilization “other commands can do this”, but it will show you which file system this directory is part of, and the utilization for the file system to which the directory belongs. it is still
handy to have a big picture of how much space left you can fill up this directory.

 

Beispiel 0x014:

List only local partitions and mount points.

 

Beispiel 0x015:

Display all available mount points.

 

Beispiel 0x016:

Display POSIX standard format which is useful when writing shell scripts.

Output:

 

Beispiel 0x017:

Did not like any of the formats and want your own format? Do not worry, df have –output option which support below fields.

Valid field names are: ‘source’, ‘fstype’, ‘itotal’, ‘iused’, ‘iavail’, ‘ipcent’,
‘size’, ‘used’, ‘avail’, ‘pcent’ and ‘target’

Suppose, I want to see partition and just mount point, use below Beispiel.

In our next post we will see about dd command.