在 Bash 中检查文件夹大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16661982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:44:53  来源:igfitidea点击:

Check folder size in Bash

bashshelldirectory

提问by tom

I'm trying to write a script that will calculate a directory size and if the size is less than 10GB, and greater then 2GB do some action. Where do I need to mention my folder name?

我正在尝试编写一个脚本来计算目录大小,如果大小小于 10GB,大于 2GB 则执行一些操作。我需要在哪里提及我的文件夹名称?

# 10GB
SIZE="1074747474"

# check the current size
CHECK="`du /data/sflow_log/`"
if [ "$CHECK" -gt "$SIZE" ]; then
  echo "DONE"
fi

回答by Mingyu

You can do:

你可以做:

du -h your_directory

which will give you the size of your target directory.

这将为您提供目标目录的大小。

If you want a brief output, du -hcs your_directoryis nice.

如果你想要一个简短的输出,那du -hcs your_directory很好。

回答by Taxellool

if you just want to see the folder size and not the sub-folders, you can use:

如果您只想查看文件夹大小而不是子文件夹,您可以使用:

du -hs /path/to/directory

Update:

更新:

You should know that dushows the used disk space; and not the file size.

您应该知道du显示已使用的磁盘空间;而不是文件大小。

You can use --apparent-sizeif u want to see sum of actual file sizes.

如果您--apparent-size想查看实际文件大小的总和,则可以使用。

--apparent-size
      print  apparent  sizes,  rather  than  disk  usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse')
      files, internal fragmentation, indirect blocks, and the like

And of course theres no need for -h(Human readable) option inside a script.

当然,-h脚本中不需要(人类可读)选项。

Instead You can use -bfor easier comparison inside script.

相反,您可以使用-b脚本内部进行更轻松的比较。

But You should Note that -bapplies --apparent-sizeby itself. And it might not be what you need.

但是你应该注意这本身就-b适用--apparent-size。它可能不是您所需要的。

-b, --bytes
      equivalent to '--apparent-size --block-size=1'

so I think, you should use --block-sizeor -B

所以我认为,你应该使用--block-size-B

#!/bin/bash
SIZE=$(du -B 1 /path/to/directory | cut -f 1 -d "   ")    
# 2GB = 2147483648 bytes
# 10GB = 10737418240 bytes
if [[ $SIZE -gt 2147483648 && $SIZE -lt 10737418240 ]]; then
    echo 'Condition returned True'
fi

回答by paddy

Use a summary (-s) and bytes (-b). You can cut the first field of the summary with cut. Putting it all together:

使用摘要 ( -s) 和字节 ( -b)。您可以使用 剪切摘要的第一个字段cut。把它们放在一起:

CHECK=$(du -sb /data/sflow_log | cut -f1)

回答by siliconrockstar

To just get the size of the directory, nothing more:

要获取目录的大小,仅此而已:

du --max-depth=0 ./directory

output looks like

输出看起来像

5234232       ./directory

回答by Michael Silverstein

To check the size of all of the directories within a directory, you can use:

要检查目录中所有目录的大小,您可以使用:

du -h --max-depth=1

du -h --max-depth=1

回答by Shivraaz

if you just want to see the aggregate size of the folder and probably in MB or GB format, please try the below script

如果您只想查看文件夹的聚合大小,并且可能是 MB 或 GB 格式,请尝试以下脚本

$du -s --block-size=M /path/to/your/directory/

回答by Gutz-Pilz

# 10GB
SIZE="10"


# check the current size
CHECK="`du -hs /media/662499e1-b699-19ad-57b3-acb127aa5a2b/Aufnahmen`"
CHECK=${CHECK%G*}
echo "Current Foldersize: $CHECK GB"

if (( $(echo "$CHECK > $SIZE" |bc -l) )); then
        echo "Folder is bigger than $SIZE GB"
else
        echo "Folder is smaller than $SIZE GB"
fi

回答by Vignesh Raja

If it helps, You can also create an alias in your .bashrcor .bash_profile.

如果有帮助,您还可以在您的.bashrc或 中创建别名.bash_profile

function dsize()
{
    dir=$(pwd)
    if [ "" != "" ]; then
            dir=
    fi
    echo $(du -hs $dir)
}

This prints the size of the current directory or the directory you have passed as an argument.

这将打印当前目录或您作为参数传递的目录的大小。