bash 脚本解析 du -s * 输出并警告超出特定存储限制的用户

发布于 2024-10-30 16:28:22 字数 803 浏览 5 评论 0原文

我正在尝试编写一个脚本来检查主文件夹磁盘使用情况,并在用户高于 xxGB 时发出警告,通过电子邮件,

我将 du -s * 的输出转储到临时文件中,逐行读取它,当我尝试从 du 的输出中读取文件夹的大小和名称,它无法正常工作,只需执行 echo $file 我将每一行转储为两行,我尝试展开以用空格替换制表符,但也不起作用,我也不知道如何根据大小进行比较。

#!/bin/bash

#echo "Disk usage report for /homes on `hostname`"

EMAIL="[email protected]"

##########################
# check staff
#########################

cd /homes/staff/
file1="/root/scripts/temp_check"
file2="/root/scripts/temp_check2"
du -s * | sort -rn | head -15  |awk '{print}' > $file1
expand $file1 > $file2

for line in $(cat $file2)

do

echo $line

# echo $line | awk '{ print $2 }'

mail -s "Disk usage report for your homefolder" $EMAIL

done

I am trying to wrote a script to check homefolders disk usage, and warn users when they are above xxGB, by email

I dump the output of the du -s * to a temp file, read it line by line, and when i try to read the size and name of the folder from the output of du it does not work correctly, just doing an echo $file i get each line dumped as two lines, i tried expand to replace the tabs with spaces, but also didn't work, and I am not sure how to do the comaprison based on size too.

#!/bin/bash

#echo "Disk usage report for /homes on `hostname`"

EMAIL="[email protected]"

##########################
# check staff
#########################

cd /homes/staff/
file1="/root/scripts/temp_check"
file2="/root/scripts/temp_check2"
du -s * | sort -rn | head -15  |awk '{print}' > $file1
expand $file1 > $file2

for line in $(cat $file2)

do

echo $line

# echo $line | awk '{ print $2 }'

mail -s "Disk usage report for your homefolder" $EMAIL

done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

找个人就嫁了吧 2024-11-06 16:28:22

为什么不简单地实施磁盘配额呢?几乎所有 Unix/Linux 系统都可以做到这一点。

然而,如果你真的想这样做,为什么还要搞这些阴谋呢?

du - s * 将生成两列输出,其中包含使用的磁盘空间和用户名。使用 while 循环而不是将所有内容放入临时文件中。

cd /home   #Or where ever all the user's home directories are stored
du -s | while read space user
do
    if [ $space -gt 10000000 ]
    then
        mailx -s"You're using a lot of diskspace!" $user <<MAIL
Dear $user:

We notice that you are now using $space in your home directory.
are you storing there? The total amount of diskspace allowed
is 15,000,000. We highly suggest you trim down your diskspace, or
we'll do it for you.

Sincerely,

Your Kindly System Administrator
MAIL
  fi
done   

Why not simply implement disk quotas? Almost all Unix/Linux systems can do that.

However, if you're really want to do it this way, why all the machinations?

The du - s * will produce a two column output with diskspace used and user name. Use a while loop instead of putting everything in temporary files.

cd /home   #Or where ever all the user's home directories are stored
du -s | while read space user
do
    if [ $space -gt 10000000 ]
    then
        mailx -s"You're using a lot of diskspace!" $user <<MAIL
Dear $user:

We notice that you are now using $space in your home directory.
are you storing there? The total amount of diskspace allowed
is 15,000,000. We highly suggest you trim down your diskspace, or
we'll do it for you.

Sincerely,

Your Kindly System Administrator
MAIL
  fi
done   
缺⑴份安定 2024-11-06 16:28:22

for 循环根据空格标记您的输入。所以每个单词都变成了 $line。

您可以使用 while 循环来正确捕获输入,而不是 for 循环,例如

cat $file2 | while read line; do echo $line; done

(您可以添加 暂时将 -x 设置为您的脚本以查看发生了什么)

The for loop is tokenizing your input based on spaces. So each word becomes a $line.

Instead of for loop, you can use a while loop to capture the input correctly, e.g.

cat $file2 | while read line; do echo $line; done

(You could add set -x to your script temporarily to see what's happening)

月寒剑心 2024-11-06 16:28:22

看看 durep

安装Ubuntu 中的 durep

使用以下命令安装 durep

sudo aptitude install durep

使用 durep

语法大致为 durep [OPTION]... [DIRECTORY]

  • “durep -w ~/durepweb -td 2″

    <块引用>

    这会将当前目录开始到深度 2 的目录树打印到控制台,并在目录 ~/durepweb 中创建网页(该目录必须存在)。

Have a look at durep

Install durep in Ubuntu

Use the following command to install durep

sudo aptitude install durep

Using durep

Syntax is roughly durep [OPTION]… [DIRECTORY]

  • “durep -w ~/durepweb -td 2″

    This would print the directory tree starting from the current directory to depth 2 to the console and also create web pages in the directory ~/durepweb (this directory must exist).

不念旧人 2024-11-06 16:28:22

如果您要求,

 du -s /home/joe/* 

您将获得所有文件(隐藏文件除外)和目录的一一摘要,因为 * 是由 shell 扩展的。

 du -s /home/joe

将为您提供一行,总结所有内容,包括隐藏文件。

 du -s . 

也会总结整个目录 - 并将包括隐藏文件(刚刚测试过)。

由于它只是一行,所以整行从 折叠

 du -s * | sort -rn | head -15  |awk '{print}' > $file1

 du -s . >$file1 

,因为您不需要对单行进行排序,将其减少到 15 行,并使用 awk 语句重复它。

If you ask for

 du -s /home/joe/* 

you get a summary for all files (except hidden files) and directories one by one, because the * is expanded by the shell.

 du -s /home/joe

will give you a single line, everything summed up, including hidden files.

 du -s . 

would summarize the whole directory too - and would include hidden files (just tested it).

Since it will just be one line, the whole line collapses from

 du -s * | sort -rn | head -15  |awk '{print}' > $file1

to

 du -s . >$file1 

because you don't need to sort a single line, reduce it to 15 lines, and repeat it with the awk-statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文