乐闻世界logo
搜索文章和话题

How to find the top 10 files and directories on a linux system?

1个答案

1

In Linux systems, identifying the largest 10 files and directories can be efficiently accomplished using the du and sort commands. I will walk you through this process step by step:

1. Finding all files and directories and calculating their sizes

First, utilize the du (disk usage) command to list the sizes of all files and directories under a specified path (e.g., / for the root directory). The --max-depth=1 option restricts the command to operate only within the current directory, preventing recursion into subdirectories. This provides a simplified search; for deeper exploration, adjust this parameter accordingly.

Command:

bash
du -ah --max-depth=1 /path/to/directory

2. Sorting the results

Next, sort the output from the du command by size using the sort command. The sort -rh option sorts results in reverse order (from largest to smallest) in human-readable format (e.g., KB, MB, GB).

Command:

bash
du -ah --max-depth=1 /path/to/directory | sort -rh

3. Retrieving the largest 10 files or directories

Finally, extract the top 10 entries from the sorted list using the head command.

Command:

bash
du -ah --max-depth=1 /path/to/directory | sort -rh | head -n 10

Example Explanation

Suppose you want to find the 10 largest files or directories under /var. Execute the following:

bash
du -ah --max-depth=1 /var | sort -rh | head -n 10

This command outputs the 10 largest files or directories within /var along with their sizes.

Summary

This approach is straightforward and leverages the robust piping and text processing capabilities of the Linux command line to quickly pinpoint the largest space-consuming files and directories. Its flexibility allows easy adaptation by modifying command parameters to suit various scenarios and requirements.

2024年8月14日 13:17 回复

你的答案