如何检查的文件大小? [英] How to check size of a file?

查看:133
本文介绍了如何检查的文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,检查0大小的,但我认为必须有检查文件的大小,而不是一个简单的方法。即 file.txt的通常是10万;如何让一个脚本检查,如果是小于90K(包括0),并利用它做些wget的一个新的副本,因为该文件是在这种情况下损坏。

目前我使用的是什么..

 如果[-n file.txt的]
然后
 回声一切都很好
其他
 邮件-sfile.txt的大小为零,请修复。myemail@gmail.com<的/ dev / null的
 #抢了wget作为备用
 wget的-c https://www.server.org/file.txt -P /根/ tmp目录--output文档= /根的/ tmp / file.txt的
 MV -f /root/tmp/file.txt /var/www/file.txt
科幻


解决方案

[-n file.txt的] 不检查它的大小,它会检查字符串 file.txt的是长度不为零,因此它总是会成功的。

如果你想说大小非零,则需要 [-s file.txt的]

要获得文件的大小,你可以使用 WC -c 获得大小(文件长度)字节:

 文件= file.txt的
=的minimumSize 90000
actualsize = $(WC -c<$文件)
如果[$ actualsize -ge $的minimumSize]。然后
    回声大小超过$的minimumSize字节
其他
    回声的大小是在$字节的minimumSize
科幻

在这种情况下,这听起来像这就是你想要的。

但仅供参考,如果你想知道该文件的磁盘使用了多少空间,你可以使用杜-k 来获得千字节大小(使用的磁盘空间)

 文件= file.txt的
=的minimumSize 90
actualsize = $(DU -k$文件|切-f 1)
如果[$ actualsize -ge $的minimumSize]。然后
    回声大小超过$的minimumSize千字节
其他
    回声大小在$千字节的minimumSize
科幻

如果您需要更多的控制输出格式,你也可以看看与stat 。在Linux上,你会喜欢的东西与stat -c'%s'的file.txt的启动,并在BSD / Mac OS X上,像与stat -f'%Z'file.txt的

I've got a script that checks for 0-size, but I thought there must be an easier way to check for file sizes instead. I.e. file.txt is normally 100k; how to make a script check if it is less than 90k (including 0), and make it do wget a new copy because the file is corrupt in this case.

What I'm currently using..

if [ -n file.txt ]
then
 echo "everything is good"
else
 mail -s "file.txt size is zero, please fix. " myemail@gmail.com < /dev/null
 # Grab wget as a fallback 
 wget -c https://www.server.org/file.txt -P /root/tmp --output-document=/root/tmp/file.txt
 mv -f /root/tmp/file.txt /var/www/file.txt
fi

解决方案

[ -n file.txt ] doesn't check its size, it checks that the string file.txt is non-zero length, so it will always succeed.

If you want to say "size is non-zero", you need [ -s file.txt ].

To get a file's size, you can use wc -c to get the size (file length) in bytes:

file=file.txt
minimumsize=90000
actualsize=$(wc -c <"$file")
if [ $actualsize -ge $minimumsize ]; then
    echo size is over $minimumsize bytes
else
    echo size is under $minimumsize bytes
fi

In this case, it sounds like that's what you want.

But FYI, if you want to know how much disk space the file is using, you could use du -k to get the size (disk space used) in kilobytes:

file=file.txt
minimumsize=90
actualsize=$(du -k "$file" | cut -f 1)
if [ $actualsize -ge $minimumsize ]; then
    echo size is over $minimumsize kilobytes
else
    echo size is under $minimumsize kilobytes
fi

If you need more control over the output format, you can also look at stat. On Linux, you'd start with something like stat -c '%s' file.txt, and on BSD/Mac OS X, something like stat -f '%z' file.txt.

这篇关于如何检查的文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆