Linux servers often host multiple users and applications simultaneously. During execution, applications such as Apache or background services may generate temporary files, especially inside directories like /tmp. Over time, these files can consume significant disk space and affect system performance.
System administrators frequently need a method to identify which user is consuming disk space and calculate the total size of files created by that user.
This guide explains how to find both individual file sizes and total disk usage for files owned by a specific user using a powerful combination of Linux commands.
Scenario
Consider a domain hosted on a Linux server.
- A user named test manages the domain.
- All domain files are stored inside the user’s home directory:
~test While running applications or scripts, temporary files may be created inside the system’s /tmp directory. To monitor storage usage, we need to calculate:
- Size of each file created by the user
- Total disk space used by all those files
The Command
The following command performs this task:
find /tmp/ -user test -type f -exec du -ch {} + This single command searches for files owned by a user and displays disk usage statistics.
Understanding the Commands
1. find Command
The find utility searches for files within a directory hierarchy.
Syntax:
find /path -options In this case:
find /tmp/ Searches inside the /tmp directory.
2. -user Option
-user test This option filters results to include only files owned by the specified user.
Replace test with any username.
Example:
-user mobile 3. -type Option
Defines the file type to search.
Common file types include:
| Option | Meaning |
|---|---|
f | Regular files |
d | Directories |
l | Symbolic links |
b | Block device files |
c | Character device files |
In our command:
-type f Only regular files are selected, excluding directories.
4. -exec Option
The -exec option runs a command on files returned by find.
Syntax:
-exec command {} + {}represents the files found.+groups files together when executing the command.
Understanding the du Command
du stands for Disk Usage. It reports how much disk space files occupy.
Command used:
du -ch Options Explained
| Option | Function |
|---|---|
-c | Displays grand total |
-h | Human-readable output (KB, MB, GB) |
This provides readable size information along with the overall total.
Why Use + Instead of ; in -exec
This is an important concept.
Using ;
-exec du -ch {} \; Runs the command separately for each file:
du -ch file1
du -ch file2
du -ch file3 Result:
- Shows size per file
- No combined total
Using + (Recommended)
-exec du -ch {} + Runs the command once:
du -ch file1 file2 file3 Result:
- Individual file sizes
- Grand total at the bottom
Example Usage
Suppose we want to check disk usage of files owned by user mobile inside:
/var/mobile/balu Command:
find /var/mobile/balu/ -user mobile -type f -exec du -ch {} + Sample Output
496K /var/mobile/balu/books/GhostStory.epub
1.4M /var/mobile/balu/books/TheWiseMansFear.epub
1.3M /var/mobile/balu/books/Wolfheart.epub
648K /var/mobile/balu/books/TalionRevenant.epub
104K /var/mobile/balu/deb/package.deb
3.0M /var/mobile/balu/deb/application.deb
293M /var/mobile/balu/ipa/appfile.ipa
6.7M /var/mobile/balu/sng/music.mp3
311M total The final line shows the total disk usage of all files owned by the user.
Practical Use Cases
This command is extremely useful for:
- Monitoring
/tmpdirectory growth - Identifying users consuming excessive disk space
- Cleaning temporary files safely
- Managing shared hosting environments
- Troubleshooting storage issues
Pro Tips
-> Run periodically using cron jobs for monitoring
-> Combine with rm carefully for cleanup automation
-> Use sudo if permissions restrict file access
Example cleanup preview:
find /tmp -user test -type f Always verify before deleting files.
Conclusion
Managing disk usage is an essential responsibility for Linux administrators. By combining the power of find and du, administrators can quickly identify how much storage a specific user consumes and prevent disk space issues before they affect system performance.
The command:
find /tmp/ -user test -type f -exec du -ch {} + provides a simple yet powerful way to monitor user-generated files and maintain server health efficiently.
If you require help, contact SupportPRO Server Admin
Partner with SupportPRO for 24/7 proactive cloud support that keeps your business secure, scalable, and ahead of the curve.
