Linux Commands – du
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed ps command in Linux which is used to display current processes.
https://cloudaffaire.com/linux-commands-ps/
In this blog post, we will discuss du command in Linux. du stands for disk usage and is used to get disk space utilization of files and directories. Normally the disk space is printed in the units of 1024 bytes, but this can be overridden with block size option. Non-integer quantities are rounded up to the next higher unit.
Linux Commands – du:
You can use du command to get the disk space usage of files and directories. With no arguments, du reports the disk space for the current directory. By default, du returns in 1024 bytes or 1 kilo-bytes format but this can be changed using –block-size option or DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
######################### ## Linux Commands | du ## ######################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##--- ## du ##--- ## du [option]... [FILE]... mkdir mydir && cd mydir ## create some files and dir echo "hello" > mysfile wget http://ipv4.download.thinkbroadband.com/10MB.zip -O mylfile dd if=/dev/zero of=myspfile bs=1 count=0 seek=2G git clone https://github.com/curran/data.git ## install git if not present du ## returns current directory disk utilization du data ## returns disk utilization of a specific directory du data/fbi/README.md ## returns disk utilization of a specific file du mylfile ## by default du returns in 1024 bytes (1 KB) format. returns 10240 KB = 10 MB export DU_BLOCK_SIZE=1 ## can be changed using environment variable. du mylfile ## returns in 1 byte format. returns 10485760 bytes = 10240 KB = 10 MB |
You can use du -a or –all options to get disk space usage of all files and directories. By default, du prints only directory disk space usage.
1 2 3 4 5 |
## du -a or --all options du data/un ## returns all directories space usage du -a data/un ## returns all directories and files space usage |
You can use du -d DEPTH or –max-depth=DEPTH options to show the total for each directory (and file if -all) that is at most MAX_DEPTH levels down from the root of the hierarchy. The root is at level 0, so ‘du –max-depth=0’ is equivalent to ‘du -s’. The ‘-s’ or ‘–summarize’ option display only a total for each argument.
1 2 3 4 5 6 7 8 9 |
## du -d DEPTH or --max-depth=DEPTH options du -d 1 ## display all directories in current level du -d 2 ## display all directories in current level + 1 level down (sub directories) du -d 3 ## display all directories in current level + 2 levels down (sub sub directories) du -s ## display a summary of current level |
You can use du -c or –total options to print a grand total of all arguments after all arguments have been processed. This can be used to find out the total disk usage of a given set of files or directories.
1 2 3 4 5 |
## du -c or --total options du data/un ## returns all directories space usage du -c data/un ## returns all directories space usage and prints 34254848 total at the end |
You can use du -h or –human-readable options to append a size letter to each size, such as ‘M’ for mebibytes. Powers of 1024 are used, not 1000; ‘M’ stands for 1,048,576 bytes. This option is equivalent to ‘–block-size=human-readable’. Use the ‘–si’ option if you prefer powers of 1000.
1 2 3 4 5 |
## du -h or --human-readable options du data/un ## returns all directories space usage du -h data/un ## returns all directories space usage in human readable format |
You can use du –apparent-size or -b or –bytes options to print apparent sizes, rather than actual disk usage. apparent size is the actual size of the data in the file. For example, suppose a file contains only “hello” word, then the apparent size of the file is 6 (including newline) but when you want to save the file in the disk, it may require anywhere from 0 to 16 KiB or more of disk space, depending on the type and configuration of the file system on which the file resides. In general, apparent size is smaller than disk size in case of most files with some exception sparse files, internal fragmentation, indirect blocks, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## du --apparent-size or -b or --bytes options du -h mysfile ## disk size, returns 4.0K du -h --apparent-size mysfile ## apparent size, returns 6 du -h myspfile ## disk size, returns 0 du -h --apparent-size myspfile ## apparent size, returns 2.0G du --apparent-size myspfile ## apparent size in bytes, 2147483648 du -b myspfile du --bytes myspfile |
You can use du -B SIZE or –block-size=SIZE options to scale the size by SIZE before printing them. You can define SIZE in integers (1,1024 etc. or 1,1000 etc. unit in bytes) or in characters (K, M, G, T, P, E, Z, Y, si). You can also use du -k or -m options to display the size in KB or MB.
1 2 3 4 5 6 7 8 9 10 11 |
## du -B SIZE or --block-size=SIZE options du mylfile ## returns 10485760 bytes du --block-size=1024 mylfile ## returns 10240 kilobytes du --block-size=M mylfile ## returns 10 megabytes du -k mylfile ## returns 10240 kilobytes du -m mylfile ## returns 10 megabytes |
You can use du -S or –separate-dirs options to exclude size of any subdirectories when reporting the size of the directory itself.
1 2 3 4 5 |
## du -S or --separate-dirs options du data/uci_ml ## returns 8084 data/uci_ml du -S data/uci_ml ## returns 0 data/uci_ml |
You can use du –files0-from=FILE option to disallow processing files named on the command line, and instead process those named in file FILE; each name being terminated by a zero byte (ASCII NUL). This is useful when the list of filenames is so long that it may exceed a command line length limitation. In such cases, running ‘du’ via ‘xargs’ is undesirable because it splits the list into pieces and makes ‘du’ print with the ‘–total’ (‘-c’) option for each sublist rather than for the entire list. One way to produce a list of ASCII NUL terminated file names is with GNU ‘find’, using its ‘-print0’ predicate. If FILE is ‘-‘ then the ASCII NUL terminated file names are read from standard input.
1 2 3 4 5 |
## du --files0-from option ## returns size of all files having extension .txt or .csv in data directory find data -name '*.txt' -print0 | du --files0-from=- find data -name '*.csv' -print0 | du --files0-from=- |
You can use du -t SIZE or –threshold=SIZE options to exclude entries based on a given SIZE. The SIZE refers to used blocks in normal mode (*note Block size::), or inodes count in conjunction with the ‘–inodes’ option. If SIZE is positive, then du will only print entries with a size greater than or equal to that. If SIZE is negative, then ‘du’ will only print entries with a size smaller than or equal to that.
1 2 3 4 5 6 7 |
## du -t SIZE or --threshold=SIZE options du -a --threshold=20MB ## returns all files and directories whose size > 20 MB du -a -t -1024 ## returns all files and directories whose size < 1024 bytes du -ah -t 1KB -t -10KB ## returns all files and directories whose size > 1 KB && < 10 KB |
You can use du –exclude=PATTERN option to skip subdirectories or files matching PATTERN.
1 2 3 4 5 |
## du --exclude=PATTERN option du -a --exclude="data*" ## excludes data directory du -a --exclude="*.csv" ## excludes all files having .csv extension |
You can use du –exclude-from=FILE option is similar to –exclude option, except take the patterns to exclude from FILE, one per line. If FILE is ‘-‘, take the patterns from standard input.
1 2 3 4 5 |
## du --exclude-from=FILE option echo "data" > mysfile ## exclude data directory du -a --exclude-from=mysfile |
You can use du –time or –time-style=STYLE options to display different timelines for the file or directories along with disk usage. –time-style=STYLE option is used with –time option to format the time using STYLE arguments which can be: full-iso, long-iso, iso, or +FORMAT; FORMAT is interpreted like in date command.
1 2 3 4 5 6 7 8 9 |
## du --time or --time-style=STYLE options du --time mysfile ## displays last mtime du --time=ctime mysfile ## displays last ctime du --time=atime mysfile ## displays last atime du --time --time-style="iso" mysfile ## displays time in YYYY-MM-DD format |
You can use du -D or -H or -L or -P options with -R option to control the behavior of the symbolic link.
- -D or -H : follow the symbolic link provided as command-line argument, but do not follow any other symbolic link during traversing
- -L : follow all symbolic links
- -P : Do not follow any symbolic link
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
## du -P or -L or -D/-H options cd .. ## crate some symbolic links rm -rf mydir cd mkdir -p ~/mydir1/mydir11 ~/mydir2 ln -s ~/mydir1 mylink1 ln -s ~/mydir2 ~/mydir1/mylink2 echo "hello" > ~/mydir1/myfile1 echo "world" > ~/mydir2/myfile2 tree ## . ## ├── mydir1 ## │ ├── mydir11 ## │ ├── myfile1 ## │ └── mylink2 -> /home/debjeet/mydir2 ## ├── mydir2 ## │ └── myfile2 ## └── mylink1 -> /home/debjeet/mydir1 du -ah mylink1 ## does not follow symbolic link du -ahP mylink1 ## does not follow symbolic link du -ahD mylink1 ## follow symbolic link in the argument and no follow thereafter du -ahL mylink1 ## always follow symbolic link |
You can use du –inodes option to list inode usage information instead of block usage. This option is useful for finding directories that contain many files and therefore eat up most of the inodes space of a file system.
1 2 3 4 5 |
## du --inodes option du --inodes ## list inode usage information rm -rf my* |
Hope you have enjoyed this article. In the next blog post, we will discuss df command in Linux.