bash 如何从shell脚本获取文件大小(以字节为单位)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18689592/
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
How to get file size in bytes from shell script?
提问by jsherk
I am trying to create a script to write an XML file for Apple's ITMSP Transporter files for uploading metadata to the App Store. Requirements for screenshots are filename, MD5 checksum and filesize in bytes.
我正在尝试创建一个脚本来为 Apple 的 ITMSP Transporter 文件编写一个 XML 文件,以便将元数据上传到 App Store。屏幕截图的要求是文件名、MD5 校验和和文件大小(以字节为单位)。
MD5 checksum is easy and be can be retrieved with md5 -q image.png
MD5 校验和很容易,可以用 md5 -q image.png
I am however having a hard time trying to get the byte size of the image file. If I use du -k image.png
command, it returns the size rounded up in kilo bytes. So for example if the actual size is 5722 bytes, du
will return 8 (as in 8K or 8192 bytes) which is not correct. And the default for du
is in 512 byte chunks but still rounds the value up (so it will return 16 instead of 8).
但是,我很难尝试获取图像文件的字节大小。如果我使用du -k image.png
命令,它会返回以千字节为单位向上舍入的大小。例如,如果实际大小为 5722 字节,du
将返回 8(如 8K 或 8192 字节),这是不正确的。并且默认为du
512 字节块,但仍将值向上舍入(因此它将返回 16 而不是 8)。
I am running Lion OSX 10.7.4.
我正在运行 Lion OSX 10.7.4。
回答by duskwuff -inactive-
One easy approach is:
一种简单的方法是:
stat -f%z image.png
stat
normally spits out a bunch of data, but the %z
format just selects the size in bytes.
stat
通常会吐出一堆数据,但%z
格式只是选择以字节为单位的大小。
回答by pixel 67
On OSX do stat -f "%z bytes"
.
在 OSX 上做stat -f "%z bytes"
。
回答by John R.
I apologize for such a late response, but I need to say this.
对于这么晚的回复,我深表歉意,但我需要说一下。
Open Finder and single clickon the file you want to measure. Then press ?I and under size, you will see the file size represented in bytes, with a other file size written in KB, MB, GB or TB will be in parenthesis next to it.
打开 Finder 并单击要测量的文件。然后按 ?I,在 size 下,您将看到以字节表示的文件大小,以 KB、MB、GB 或 TB 表示的其他文件大小将在旁边的括号中。
This appears when you press ?I:
当您按 ?I 时会出现此信息: