Linux du command represents disk usage by files and directories on a disk. du command can be used in several variations to check utilization of space on a Linux systems. We will discuss few different variation of du command but fist basic use of du command.
Here on entering just du at prompt on terminal window, you will be presented with list of file names of files in current working directory along with space occupies by file. Finally at bottom total file size will be given.
Use of du command in Linux with examples
Find disk space usage by particular file or folder
To find Find disk space usage by particular file or folder, just input du command along with path file or folder. You will be presented with file size along will file name in case of particular file is targeted. In case directory is mentioned a list of file and their sizes will be presented.
du /path/to/file _or_directory
du test.txt du /home/test/
Display disk usage in different size formats
Similar to disk free command df command du command can also display file sizes in in Kilobyte, Megabyte and Gigabyte etc.
Display disk usage in Kilobytes
Using -BK switch with du command displays size with basic block size of 1024 bytes.
Display disk usage in Megabytes
Using -BM switch with du command displays size with basic block size of 1 Megabyte.
Display disk usage in Gigabytes
Using -BG switch with du command displays size with basic block size of 1 Gigabytes.
There are few more options available Terabyte( use switch –BT), Petabyte( use switch –BP), Exabyte( use switch –BE), Zetabyte and Yottabyte.
Check filesystem disk usage in more usable format (Human readable)
When du command is used with switch –h, it displays size human readable format. By human readable format we mean most suitable file size. For example a folder may have files ranging in size from few bytes to several GB’s. On using –h switch command will show file size using most appropriate unit of file size.
View only summary of space usage for a file or folder
To view space usage by a particular file or folder i.e. output of only space usage without list of files or folders use switch –s. This switch will present only summary in its output.
View disk usage for all files and directories
Use on –a switch will shown file size of all directories and files within working directory including sub directories.
File size will be shown in Kilobyte.
To view disk usage for all files and directories in human readable format use –h switch with –a switch.
Exclude certain file type
You can use exclude switch if you want to exclude files matching certain pattern.
du –ah –exclude=”*.pdf” /path/to/folder