The du (Disk Usage) commands are most handy in Linux
du
is a command line tool provided with Linux, which reports the amount of disk space used by directories and files. du
stands for 'disk usage'. This is the main tool for analyzing disk space in the command line.
How to use du command in Linux
- Basic usage
- Control output
- Change the capacity display
- Include files and links
- Exclude files, directories and links
Basic usage
du /path/to/directory
Run du
with a directory to see the full analysis of the disk space that each folder holds. Each directory will be listed in turn in the standard output, so large directories or full system scans can generate tens of thousands of result streams and take a considerable amount of time. As a result, basic du
commands are often added with flags (which will be shown below). Any of these flags can be combined with another flag, although some combinations are mutually exclusive because they are contradictory.
Control output
du -c
Display a line at the bottom of the output to indicate the total amount of disk space used by the scanned folders.
du -s
Show only a summary of the total amount of disk space used by the specified directory. No output is displayed until the calculation process is complete.
du > du-report.txt
People who use the command line will recognize this command, but not everyone. This command outputs the result of the command to a text file in the current directory, named 'du-report.txt'.
du | less
This command produces cleaner and more readable results. You can browse content with fewer navigation shortcuts.
Change the capacity display
Note that the file size will usually be rounded when specific units are specified. For example, the 4KB file will be displayed as 0MB if the -m flag is called, while the 750KB file will be displayed as 1MB.
du -h
Displayed in a readable format with the appropriate volumes listed, such as kilobytes, megabytes and gigabytes, instead of the standard block (block) size.
du -k
Displays the number of blocks in 1024-byte blocks (1 kilobyte).
du -m
Displays the number of blocks in blocks of 1,048,576 bytes (1 megabyte).
du -g
Displays the number of blocks in blocks 1,073,741,824 bytes (1 gigabyte).
Include files and links
du -L
Follow the symbolic links in the command line and file hierarchy. On the other hand, the space occupied by the symbolic link itself will be reported (usually the minimum in the file system) rather than the directory tree that the symbolic link points to.
du -a
Displays the disk usage of all files, not just folders.
du /path/to/file.txt
Displays the disk usage of a specific file, as named in the command.
Exclude files, directories and links
du -X FILE
Exclude files that match any pattern in the specified string.
du --exclude="*.o"
Exclude files and subdirectories according to the pattern. In this example, all folders including the '* .o' string will be ignored. Note that these are pattern shells, not regular expressions. Thus, the control characters are limited to * , matching any string with 0 or more characters and ?
, matches any one character. The above command will exclude these files from the directory capacity calculation process. If flag -a is used, the excluded file will be ignored in the output.
du --threshold=SIZE
Exclude items that are less than SIZE if it is a positive value or items greater than SIZE if it is a negative value. SIZE is an integer and has one unit (optional). For example, the --threshold=1MB
command will ignore all files smaller than 1 megabyte (1000 ^ 2 bytes). Units including K, M, G, T, P, E, Z, Y represent kibibyte, mebibyte, gibibyte, etc. . or KB, MB, GB, TB, represent kilobytes, megabytes, gigabytes, etc. This can be a useful tool to find the largest files on the system with a command like du --threshold=1GB
.
du -d N
Set the maximum depth for N folders. This flag can take any positive integer. With this setting, du
will scan up to 2 subdirectories in the specified directory. If other subdirectories exist, they will not be scanned individually. Instead, their value will be included in the reported directories. Note that -d 0 will report the same result as the -s flag .
For example, consider the directory path 'dir1 / dir2 / dir3 / dir4', containing 1 parent directory and 3 subdirectories. The command du -d 2 dir1
will scan to dir3. Dir3 will include files in dir4, although dir4 is not listed separately.
The du
command is du
useful when paired with other utilities, such as the du -a / | sort -n -r | head -n 10
command du -a / | sort -n -r | head -n 10
du -a / | sort -n -r | head -n 10
. This command will search your entire file system ( du -a / ), sort results by size ( sort -n -r ), and then display only the top 10 results ( head -n 10 ). Basically, this is a shortcut to the 10 largest files on your computer. Combine du
with other commands via pipes to produce even more useful results.
Hope you are succesful.
You should read it
- How to use the history command in Linux
- How to use the vmstat command on Linux
- How to run a program periodically using the Watch command on Linux
- Tail command in Linux
- 20+ essential Linux security commands
- Instructions for using find command in Linux
- How to use the stat command on Linux
- The Cat command in Linux
May be interested
- Useful commands in Unix / Linuxthis chapter lists the commands, including syntax and brief descriptions. for more details about these commands, you use.
- Basic Linux commands everyone needs to knowwhen using linux, you should also learn basic linux commands to make operations quicker and easier. here are some basic commands on linux you should know.
- Top 10 examples of Netstat commands on Linuxnetstat is a command line utility that can be used to list all network connections (sockets), allowing comparison with network connections, routing tables, interface records, connection spoofing.
- Kali Linux commands from AZ and commonly used commandscombining kali linux commands from az comes with some commonly used kali linux commands that make it easy to work with this operating system.
- How to run 2 or more Terminal commands at the same time on Linuxif you use linux, you probably know how to use useful commands to work with files, install software and launch programs. however, one thing you don't know is that you can still run multiple commands at the same time.
- Basic Linux commands everyone needs to know - Operations on Linux are much fasterwhen using linux, you should also learn basic linux commands to perform operations more quickly and easily. below are some basic linux commands you should know.
- 10 most useful Linux commandsfor those who are new to linux, administration will face many difficulties. here are the 10 most useful linux commands that make your administration a lot easier.
- How to create disk full BAT file repair Windows 10full disk error 100% disk usage windows is no stranger to computer users. there are many ways to overcome this situation, in which you can create bat files to fix simple full disk errors.
- How to run Linux commands when starting Windows Subsystem for Linux on Windows 10the latest beta version of windows 10 allows users to automatically run linux commands when windows subsystem for linux starts.
- Try Disk Usage, a new tool to analyze hard drive space on Windows 10disk usage is located in c: windowssystem32diskusage.exe and will display the user guide when entering diskusage / ?. this utility can scan your entire drive or specified folders and report how many each folder is in use.