tom*_*tom 283 directory bash shell
我正在尝试编写一个计算目录大小的脚本,如果大小小于10GB,则大于2GB的脚本会执行某些操作.我在哪里需要提及我的文件夹名称?
# 10GB
SIZE="1074747474"
# check the current size
CHECK="`du /data/sflow_log/`"
if [ "$CHECK" -gt "$SIZE" ]; then
echo "DONE"
fi
Run Code Online (Sandbox Code Playgroud)
Min*_*gyu 564
你可以做:
du -h your_directory
Run Code Online (Sandbox Code Playgroud)
这将为您提供目标目录的大小.
如果你想要一个简短的输出,du -hcs your_directory很好.
Tax*_*ool 134
如果您只想查看文件夹大小而不是子文件夹,可以使用:
du -hs /path/to/directory
Run Code Online (Sandbox Code Playgroud)
更新:
您应该知道du显示已用磁盘空间; 而不是文件大小.
如果您--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
Run Code Online (Sandbox Code Playgroud)
当然,-h脚本中不需要(人类可读)选项.
相反,您可以使用-b更简单的内部比较.
但是你应该注意它本身-b适用--apparent-size.它可能不是你需要的.
-b, --bytes
equivalent to '--apparent-size --block-size=1'
Run Code Online (Sandbox Code Playgroud)
所以我想,你应该使用--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
Run Code Online (Sandbox Code Playgroud)
pad*_*ddy 26
使用summary(-s)和bytes(-b).您可以使用剪切摘要的第一个字段cut.把它们放在一起:
CHECK=$(du -sb /data/sflow_log | cut -f1)
Run Code Online (Sandbox Code Playgroud)
sil*_*tar 24
要获得目录的大小,仅此而已:
du --max-depth=0 ./directory
Run Code Online (Sandbox Code Playgroud)
输出看起来像
5234232 ./directory
Run Code Online (Sandbox Code Playgroud)
小智 11
如果您只想查看文件夹的聚合大小,可能是MB或GB格式,请尝试以下脚本
$du -s --block-size=M /path/to/your/directory/
Run Code Online (Sandbox Code Playgroud)
要检查目录中所有目录的大小,可以使用:
du -h --max-depth=1
# 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
Run Code Online (Sandbox Code Playgroud)
.bashrc如果有帮助,您还可以在您的或中创建别名.bash_profile。
function dsize()
{
dir=$(pwd)
if [ -n "$1" ]; then
dir=$1
fi
du -hs "$dir"
}
Run Code Online (Sandbox Code Playgroud)
这将打印当前目录或您作为参数传递的目录的大小。
| 归档时间: |
|
| 查看次数: |
239912 次 |
| 最近记录: |