The day-to-day tasks of the sysadmin are always different for everyone; however, there are simple tasks that are executed equally on managed systems. In the days when disk space was a risk factor in the administrator's day, it was vitally important to locate the directory or filesystem to debug.

Nowadays, in virtual machines mainly, it's just as important to have the managed systems healthy and with space available for the execution of their processes and logging in their log files.

[ You might also like: Linux scripting: 3 how-tos for while loops in Bash ]

The best way to start is to use the simplest commands by linking their outputs as inputs to a new execution, forming what is known as a one-liner. That is, using the command line interpreter to read the standard output of a command and put it as the input variable of the next command, for this, the xargs command helps receive that variable and execute it.

Check out a practical example:

Ticket RH0502201-1: The prodenv production server sends "low disk space on root filesystem" alerts. The filesystem purge is requested.

  1. Access the server and check the space in /filesystem:


# df -h /

Filesystem                     Size  Used  Avail Use% Mounted on

/dev/mapper/prodenv--vg-root   720G  720G  0     100% /
  1. Change to the root directory, list the directories, and to calculate the disk space used in each directory, use a pipe and xargs to send the ls command output to du command:

# ls | xargs du -sk
  1. With a pipe again, use the sort command to list the output from lowest to highest disk space used in the directories:


# ls | xargs du -sk | sort -n
  1. For this case, only the directories with the most disk space occupied are needed, so we could limit the list to the last five directories in the list. Using another pipe and the tail command:


# ls | xargs du -sk | sort -n | tail -5

Note: If you want to avoid the error message where the du command could not access, send the standard error output to the /dev/null device:


# ls | xargs du -sk 2> /dev/null | sort -n | tail -5
  1. With the list of directories defined, use AWK to create a new sorted list of directories, column two of the original list:


# ls | xargs du -sk 2> /dev/null | sort -n | tail -5 | awk '{ print $2 }'
  1. The sorted list is reentered as a variable to du to show the space used in a humanly readable format. Use another pipe and xargs:


# ls | xargs du -sk 2> /dev/null | sort -n | tail -5 | awk '{ print $2 }' | xargs du -sh
  1. Validate the feasibility of deleting or compressing the files that are overwhelming the filesystem to free up as much disk space as possible.

Probably it is possible to avoid steps 5 and 6 and execute the cleaning directly; however, in many cases, these directories belong to some application and notify the responsible party of it to perform the cleaning or simply as evidence of the executed process for the issue resolution documentation.

Finally, our one-liner looks like the following:


​​​​​​​# ls | xargs du -sk 2> /dev/null | sort -n | tail -5 | awk '{ print $2 }' | xargs du -sh

This one-liner is part of my arsenal used in the day-to-day tasks as a sysadmin, I hope you find it useful. How about you? What one-liners do you have up your sleeve?

[ Get this free ebook: Managing your Kubernetes clusters for dummies. ]


关于作者

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来