One of the common Linux interview question to developer is "Print top 5 largest directories in Linux using du command". In the past, I have shared 30 UNIX command interview questions and Linux interview questions and today, we will discuss this problem and learn what to do if it comes on interview, or more likely you face this in your day to day work as developer, IT support or System administrator. If Interviewer ask you to print top 5 biggest directories by size instead of files then you can use the du (disk usage) command to find directories. The du command will list down size of every file and directory and then you can use the sort command to sort them in descending order by using -n and -r option.
The -n option sort numerically and -r reverses the order. Once we have list of files and directories from biggest to smallest, you can just use the head command to take the top 5 or top 10 largest files and directories.
Here is the complete Linux command you can run on any directory you want to find the top 5 or 1op 10 largest files:
$ du -a . | sort -n -r | head -n 10
16095096 .
13785288 ./logs
6095380 ./logs/app
2125252 ./temp
2125244 ./temp/data
2125240 ./temp/data/app
The du command list both files and directories e.g. if you run above command in the logs directory it would print the catalina.out file as largest followed by any directory.
As I said, finding largest file is simple if you know the directory, you can just use the ls -lS command to list files by their size in reverse order, but the task become difficult when you have to search the largest file or directory from the start of file system or across sub-directories.
I often use this question to check if a candidate is familiar with essential Linux commands and concepts or not. As in order to solve this problem, candidate must know about find, du, sort and head command then he also needs to be aware of different options e.g. file -type, sort -nr etc.
Candidate also need to know how to use pipe to chain multiple commands. So, in one question you can gauge if has some practical knowledge of Linux or not.
If you are new in Linux, here is also a nice list of most essential Linux commands from Alex Xu and ByteByteGo, you can print and keep it reference for easy access:
That's all about how to find the top 5 largest files and directories by size in Linux. This is quite useful command so if you want to keep it handy, you can also copy this in your work book as you will need it sooner or later while working as a Java developer, DevOps Engineer, IT support guy or system administrator.
Other Linux command tutorials and examples you may like:
- My favorite tips to work fast in Linux (tips)
- How to get an IP address from the hostname and vice-versa in Linux (command)
- 5 Best courses to learn Bash Scripting (courses)
- 10 examples of the xargs command in Linux (examples)
- 5 Free Courses to learn Linux for Beginners (Free courses)
- 10 examples of date command in Linux (examples)
- How to create, update and delete soft link in UNIX (command)
- 10 examples of Vim in UNIX (examples)
- My Favorite Courses to learn VIM Editor in-depth (courses)
- 10 examples of cut command in Linux (examples)
- 5 examples of sort command in Linux (examples)
- 6 Free Courses to learn Bash scripting in-depth (free courses)
- 5 examples of kill command in Linux (examples)
- 10 Books every Linux Power user should read (books)
Thank you for reading this article so far. If you have any questions or doubt then feel free to ask in comments.
No comments :
Post a Comment