When dealing with disk usage (du
) in Linux, it’s often helpful to sort the output by size to quickly identify the largest directories. Follow these steps to accomplish this:
Run the du -h
Command:
- Open the Terminal:
- Open a terminal window on your Linux system. You can do this using the terminal emulator available on your desktop environment.
- Run the
du -h
Command:- Use the
du -h
command to display disk usage information in human-readable format (sizes in KB, MB, or GB).
- Use the
- Sort the Output by Size:
- Pipe the output of
du -h
to thesort
command, specifying the-h
flag to handle human-readable sizes correctly.
du -h | sort -h
- Pipe the output of
- Reverse the Sort Order (Optional):
- If you want to see the largest directories at the top, add the
r
flag to thesort
command.
du -h | sort -rh
- If you want to see the largest directories at the top, add the
- Display Top N Results (Optional):
- To limit the output to the top N results (e.g., top 10), use the
head
command.
du -h | sort -rh | head -n 10
- To limit the output to the top N results (e.g., top 10), use the
- Incorporate into a Script (Optional):
- If you frequently need to check disk usage, consider creating a shell script that automates these steps for convenience.
/bin/bash du -h | sort -rh | head -n 10
By following these steps, you can efficiently analyze disk usage on your Linux system and identify the largest directories. Customizing the command for your needs and incorporating it into scripts can further enhance your workflow.