While working on LINUX, there might come a situation when you want to transfer a set of files or the entire directory. In such a case, you might wanna know the disk space consumed by that particular directory or set of files. As you are dealing with LINUX, there exists a command line utility for this also which is du command that estimates and displays the disk space used by files.
So, in simple words du command-line utility helps you to find out the disk usage of set of files or a directory.
Here’s the syntax of du command :
//syntax of du command du [OPTION]... [FILE]... or du [OPTION]... --files0-from=F
where OPTION refers to the options compatible with du command and FILE refers to the filename of which you wanna know the disk space occupied.
Using du command
Suppose there are two files say kt.txt and pt.txt and you want to know the disk usage of these files, then you can simply use du command by specifying the file names along with it as:
//using du command $du kt.txt pt.txt 8 kt.txt 4 pt.txt /* the first column displayed the file's disk usage */
So, as shown above du displayed the disk space used by the corresponding files.
Now, the displayed values are actually in the units of the first available SIZE from – -block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables and if not in this format then units are default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
Don’t get puzzled from the above paragraph. We can simply use -h option to force du to produce the output in the human readable format.
Options for du command
- -a, – -all option : This option produces counts as output for all files, not for just directories.
- – -apparent-size option : This prints the apparent sizes for the files and not the disk usage which can be larger due to holes in files (sparse), internal fragmentation and indirect blocks but in real the apparent size is smaller.
- -c, – -total option : This displays a grand total.
- -B, – -block-size=SIZE option : This option causes the size to scale by SIZE like -BM prints the size in Megabytes.
- -b, – -bytes option : This option is equivalent to – -apparent-size – -block-size=1.
- -D, – -dereference-args option : This option is used to dereference only the symbolic links listed on the command line.
- -H option : This option is equivalent to the above -D option.
- – -files0-from=F option : This is used to summarize disk usage of the NUL-terminated file names specified in the file F and if the file F is “-” then read names from the standard input.
- -h, – -human-readable option : This prints the sizes in human readable format i.e in rounding values and using abbreviations like 1 K and this is the most often used option with du.
- – -si option: This is much similar to the -h option but uses power of 1000 and not of 1024.
- -k option : its equivalent to – -block-size=1K.
- -l, – -count-links option : This count sizes many times if files are hard-linked.
- -m option : This is equivalent to – – block-size=1M.
- -L, – -dereference option : This option dereferences all symbolic links.
- -P, – -no-dereference option : This option tells du not to follow any symbolic links which is by default setting.
- -0, –null option : This ends each output line with 0 byte rather than a newline.
- -S, – -separate-dirs option : This causes the output not to include the size of subdirectories.
- -s, – -summarize option : This option will allow to display a total only for each argument.
- -x, – -one-file-system option : This will cause du to skip directories on different file systems.
- -X, – -exclude-from=FILE option : Exclude files that match any pattern given in FILE.
- – -exclude=PATTERN option : It will exclude files that match PATTERN.
- -d, – -max-depth=N option : Print the total for a directory (or file, with –all) only if it is N or fewer levels below the command line argument; –max-depth=0 is the same as –summarize.
- – -time option : This will show the time of the last modification of any file in the directory, or any of its subdirectories.
- – -time=WORD option : This shows time as WORD instead of modification time :atime, access, use, ctime or status.
- – -time-style=STYLE option : this shows time using STYLE: full-iso, long-iso, iso, or +FORMAT (FORMAT is interpreted like the format of date).
- – -help option : This will display a help message and exit.
- – -version option : This will display version info and exit.
Examples of using du command
1. Using -h option : As mentioned above, -h option is used to produce the output in human readable format.
//using -h with du $du -h kt.txt pt.txt 8.0K kt.txt 4.0K pt.txt /*now the output is in human readable format i.e in Kilobytes */
2. Using du to show disk usage of a directory : Now, if you will pass a directory name say kartik as an argument to du it will show the disk usage info of the input directory kartik and its sub-directories (if any).
/*using du to display disk usage of a directory and its sub-directories */ $du kartik 4 kartik/thakral 24 kartik
Above the disk usage info of the directory kartik and its sub-directory thakral is displayed.
3. Using -a option : now, as seen above only the disk usage info of directory
kartik and its sub-directory thakral is displayed but what if you also want to know the disk usage info of all the files present under the directory kartik. For this, use -a option.
//using -a with du $du -a kartik 8 kartik/kt.txt 4 kartik/pt.txt 4 kartik/pranjal.png 4 kartik/thakral.png 4 kartik/thakral 24 kartik /*so with -a option used all the files (under directory kartik) disk usage info is displayed along with the thakral sub-directory */
4. Using -c option : This option displays the grand total as shown.
//using -c with du $du -c -h kt.txt pt.txt 8.0K kt.txt 4.0K pt.txt 12.0K total /* at the end total is displayed for the disk usage */
5. Using – -time option : This option is used to display the last modification time in the output of du.
//using --time with du $du --time kt.txt 4 2017-11-18 16:00 kt.txt /*so the last modification date and time gets displayed when --time option is used */
6. Using – -exclude=PATTERN option : In one of the example above, all the files disk usage related info was displayed of directory kartik. Now, suppose you want to know the info of .txt files only and not of .png files, in that case to exclude the .png pattern you can use this option.
//using --exclude=PATTERN with du $du --exclude=*.png -a kartik 8 kartik/kt.txt 4 kartik/pt.txt 4 kartik/thakral 24 kartik /*so, in this case .png files info are excluded from the output */
7. Using – -max-depth=N option : Now, this option allows you to limit the output of du to a particular depth of a directory.
Suppose you have a directory named FRIENDS under which you have sub-directories as FRIENDS/college and FRIENDS/school and also under sub-directory college you have another sub-directory as FRIENDS/college/farewell then you can use – -max-depth=N option in this case as:
//using --max-depth=N with du $du --max-depth=0 FRIENDS 24 FRIENDS /* in this case you restricted du output only to top=level directory */
Now, for sub-directories college and school you can use :
$du --max-depth=1 FRIENDS 16 FRIENDS/college 8 FRIENDS/school 24 FRIENDS
Now, for FRIENDS/college/farewell you can use –max-depth=2 as:
$du --max-depth=2 FRIENDS 4 FRIENDS/college/farewell 16 FRIENDS/college 8 FRIENDS/school 24 FRIENDS /*so this is how N in --max-depth=N is used for levels */
8. Using – -files0-from=F option : As mentioned above, this is used to summarize disk usage of the NUL-terminated file names specified in the file F and if the file F is “-” then read names from the standard input.
Let’s use this option for taking input from STDIN as:
//using --files0from=F with du $pwd /home/kartik $ls kt.txt pt.txt thakral /*now use this option for taking input from STDIN */ $du --files0-from=- kt.txt8 kt.txt pt.txt4 pt.txt /* in this case after giving kt.txt as a input from STDIN there is need to press Ctrl+D twice then the output is shown and same for pt.txt or any other file name given from STDIN */
Applications of du command
- It can be used to find out the disk space occupied by a particular directory in case of transferring files from one computer to another.
- du command can be linked with pipes to filters.A filter is usually a specialized program that transforms the data in a meaningful way.
- There also exists some other ways like df command to find the disk usage but they all lack du ability to show the disk usage of individual directories and files.
- It can also be used to find out quickly the number of sub-directories present in a directory.
Example of using du with filters
Let’s take a simple example of using du with sort command so that the output produced by du will be sorted in the increasing order of size of files.
$du -a kartik 8 kartik/kt.txt 4 kartik/pt.txt 4 kartik/pranjal.png 4 kartik/thakral.png 4 kartik/thakral 24 kartik /*now using du to produce sorted output */ $du -a kartik | sort -n 4 kartik/pt.txt 4 kartik/pranjal.png 4 kartik/thakral.png 4 kartik/thakral 8 kartik/kt.txt 24 kartik /* now the output displayed is sorted according to the size */
The sort command along with -n option used causes to list the output in numeric order with the file with the smallest size appearing first.
In this way du can be used to arrange the output according to the size.
That’s all about du command.
- while command in Linux with example
- who command in Linux
- od command in Linux with example
- 'dd' command in Linux
- SED command in Linux | Set 2
- rcp Command in Linux with examples
- tar command in Linux with examples
- bc command in Linux with examples
- help Command in Linux with examples
- whoami command in Linux with example
- tac command in Linux with Examples
- rm command in Linux with examples
- wc command in Linux with examples
- less command in Linux with Examples
- until command in Linux with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.