Support > About independent server > Analyzing disk space using the top command in Linux system management
Analyzing disk space using the top command in Linux system management
Time : 2025-01-20 15:25:08
Edit : Jtti

You can run the top command to view additional information about the current system status and usage, such as uptime, average load, and total number of processes. If the installed memory usage is displayed, the top 15 processes are:

# top -b -o +%MEM | head -n 22

The output must be sorted in descending order using +%MEM (note the plus sign), option b is to run top in batch mode, o is to specify the sorting process field, head uses the program to display the first few lines of the file, and option n indicates the number of lines to display.

If using top output to redirect or save to a file in Linux:

# top -b -o +%MEM | head -n 22 > topreport.txt

The top utility provides more dynamic information when it lists processes on a Linux system.

Sort by RAM/CPU usage in Linux:

# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

The Ps o/format option supports user-specified output formats, such as PID, PPID, and CMD. sort can be used to sort, which is sorted in ascending order by default. Monitoring Linux servers is one of the jobs of a system administrator. It is necessary to regularly check which files/folders are taking up more disk space to find unwanted junk files and free them from the hard disk.

Find the largest directory/file in Linux:

# du -a /home|sort -n -r|head -n 5

The top 5 directories in the partition can be found in the output.

If you want to display the largest directory in the current working directory:

# du -a | sort -n -r | head -n 5

The du option is used to estimate the file space usage, a is to display all files and folders, sort is used to sort text files, n can be compared by string values, r is to invert the comparison results, head is the first part of the output file, and n is the first n lines of printing. If you need to output the file in a readable format:

# du -hs * | sort -rh | head -5

The above command will display the top-level directories that take up more disk space. If you feel that there are unimportant directories, you can delete them to free up space. Display the maximum folder/directory:

# du -Sh | sort -rh | head -5

Find only the maximum file size:

# find -type f -exec du -Sh {} + | sort -rh | head -n 5

To find the largest file in a specific location:

# find /home/tecmint/Downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 5

or

# find /home/tecmint/Downloads/ -type f -printf "%s %p\n" | sort -rn | head -n 5

Relevant contents

Steps and uses for configuring multiple IP addresses on a Singapore server What are the best distributions of Linux based on Debian What areas are the hard disks of a server usually divided into The complete process of installing LXC in RHEL Pssh implements simultaneous commands on multiple remote linux servers Recommended enterprise-level server configuration for film and television enterprises In Linux, users are forced to change their passwords the next time they log in The powerful function and application of open source relational database management system Install MinGW-w64 on Windows10 Process for deploying the springboot project on a cloud server
Go back

24/7/365 support.We work when you work

Support