How do I find the top 10 file method totals in Linux

  • 2020-11-18 06:34:59
  • OfStack

preface

It is well known that when your system runs out of disk space, you might use the df, du, or ncdu commands to check, but these commands only show files in the current directory, not system-wide files.

It takes you a lot of time to get the largest file in the system with the above command, because you have to go into each directory and run the above command repeatedly.
This approach is cumbersome and inappropriate.

If so, how do you find the top 10 files in Linux?

I searched Google for a long time, but found no similar articles. Instead, I saw many articles that listed the top 10 files in my current directory. So, I hope this article will be helpful to those who have similar needs.

In this tutorial, we will show you how to find the top 10 largest files in the Linux system using the following four methods.

Method 1

There is no specific command in Linux that can do this directly, so we need to combine multiple commands.


# find / -type f -print0 | xargs -0 du -h | sort -rh | head -n 10
 
1.4G /swapfile
1.1G /home/magi/ubuntu-17.04-desktop-amd64.iso
564M /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqTFU0XzkzUlJUZzA
378M /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqeldzUmhPeC03Zm8
377M /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqRGd4V0VrOXM4YVU
100M /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
93M /usr/lib/firefox/libxul.so
84M /var/lib/snapd/snaps/core_3604.snap
84M /var/lib/snapd/snaps/core_3440.snap
84M /var/lib/snapd/snaps/core_3247.snap

A:

find: Command to search for files in the directory structure / : Search throughout the system (starting from the root directory) -ES35en: Specifies the file type
f: General files -print0: Displays the full file name followed by a null character in standard output (null) | : control operator that passes the output of one command to the next for further processing xargs: A command that converts standard input into command-line arguments -0: The record is split with empty characters (null) instead of white space characters (LCTT) du-h: Command to calculate disk space usage in a readable format sort: A command to sort text files -ES46en: Reverse the result -ES47en: Print the output in a readable format head: Prints the command at the beginning of the file n-10: Prints the first 10 files

Method 2

This is another way to find the top 10 largest files in the Linux system. We still use multiple commands to accomplish this task.


# find / -type f -exec du -Sh {} + | sort -rh | head -n 10
 
1.4G /swapfile
1.1G /home/magi/ubuntu-17.04-desktop-amd64.iso
564M /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqTFU0XzkzUlJUZzA
378M /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqeldzUmhPeC03Zm8
377M /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqRGd4V0VrOXM4YVU
100M /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
93M /usr/lib/firefox/libxul.so
84M /var/lib/snapd/snaps/core_3604.snap
84M /var/lib/snapd/snaps/core_3440.snap
84M /var/lib/snapd/snaps/core_3247.snap

A:

find: The command to search for files in the directory structure / : Search throughout the system (starting from the root directory) -ES63en: Specifies the file type
f: General files -ES66en: Runs the specified command on the selected file du: Command to calculate the disk space occupied by a file -ES68en: Does not contain the size of the subdirectory -ES69en: Printed in a readable format {} : Recursively search the directory and count the disk space taken up by each file | : control operator that passes the output of one command to the next for further processing sort: Command to sort text files by line -ES71en: Reverse the result -ES72en: Print the output in a readable format head: Prints the command at the beginning of the file n-10: Print the first 10 files

Methods 3

Here is another method for searching the top 10 files in the Linux system.


# find / -type f -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}
 
84M /var/lib/snapd/snaps/core_3247.snap
84M /var/lib/snapd/snaps/core_3440.snap
84M /var/lib/snapd/snaps/core_3604.snap
93M /usr/lib/firefox/libxul.so
100M /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
377M /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqRGd4V0VrOXM4YVU
378M /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqeldzUmhPeC03Zm8
564M /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqTFU0XzkzUlJUZzA
1.1G /home/magi/ubuntu-17.04-desktop-amd64.iso
1.4G /swapfile

A:

find: The command to search for files in the directory structure / : Search throughout the system (starting from the root directory) -ES87en: Specifies the file type
f: General files -print0: Output the full file name followed by a null character (null) | : control operator that passes the output of one command to the next for further processing xargs: A command that converts standard input into command-line arguments -0: Records are split with empty characters (null) instead of white space characters du: Command to calculate the disk space occupied by a file sort: Command to sort text files by line -ES96en: Compare by number size tail-10: The command to output the end of the file (the last 10 files) cut: The command to delete a specific section from each line -f2: Select only specific field values -ES100en {} : Replace each replacement string that appears in the initial argument with a name read from standard input -s: Displays only the sum of each parameter -h: Print the output in a readable format {} : Recursively search the directory and count the disk space taken up by each file

Methods 4

There is also a method for finding the top 10 largest files in the Linux system.


# find / -type f -ls | sort -k 7 -r -n | head -10 | column -t | awk '{print $7,$11}'
 
1494845440 /swapfile
1085984380 /home/magi/ubuntu-17.04-desktop-amd64.iso
591003648 /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqTFU0XzkzUlJUZzA
395770383 /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqeldzUmhPeC03Zm8
394891761 /home/magi/.gdfuse/magi/cache/0B5nso_FPaZFqRGd4V0VrOXM4YVU
103999072 /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
97356256 /usr/lib/firefox/libxul.so
87896064 /var/lib/snapd/snaps/core_3604.snap
87793664 /var/lib/snapd/snaps/core_3440.snap
87089152 /var/lib/snapd/snaps/core_3247.snap

A:

find: The command to search for files in the directory structure / : Search throughout the system (starting from the root directory) -ES116en: Specifies the file type
f: General files -ES119en: Lists the current file in standard output as ls-ES121en | : control operator that passes the output of one command to the next for further processing sort: Command to sort text files by line -ES123en: Sort by the specified column -ES124en: Reverse the result -ES125en: Compare by number size head: Output the command at the beginning of the file -10: Print the first 10 files column: Format its input into multi-column commands -ES128en: Determines the number of columns the input contains and creates a table awk: Schema scanning and processing language '{print $7,$11}' : Prints only the columns specified

conclusion


Related articles: