Linux 在shell中获取文件大小(以字节为单位)的便携式方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1815329/
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-08-03 17:56:45  来源:igfitidea点击:

Portable way to get file size (in bytes) in shell?

linuxbashshellsolaris

提问by

On Linux, I use stat --format="%s" FILE, but Solaris I have access to doesn't have stat command. What should I use then?

在 Linux 上,我使用stat --format="%s" FILE,但我有权访问的 Solaris 没有 stat 命令。那我应该用什么?

I'm writing Bash scripts, and can't really install any new software on the system.

我正在编写 Bash 脚本,并且无法在系统上真正安装任何新软件。

I've considered already using:

我已经考虑过使用:

perl -e '@x=stat(shift);print $x[7]' FILE

or even:

甚至:

ls -nl FILE | awk '{print }'

But neither of these looks sensible - running Perl just to get file size? Or running 2 commands to do the same?

但是这些看起来都不明智——运行 Perl 只是为了获取文件大小?或者运行 2 个命令来做同样的事情?

采纳答案by Carl Smotricz

wc -c < filename(short for word count, -cprints the byte count) is a portable, POSIXsolution. Only the output format might not be uniform across platforms as some spaces may be prepended (which is the case for Solaris).

wc -c < filename(字数的缩写,-c打印字节数)是一种可移植的POSIX解决方案。只有输出格式在平台上可能不统一,因为可能会预先设置一些空格(Solaris 就是这种情况)。

Do not omit the input redirection. When the file is passed as an argument, the file name is printed after the byte count.

不要省略输入重定向。当文件作为参数传递时,文件名在字节数之后打印。

I was worried it wouldn't work for binary files, but it works OK on both Linux and Solaris. You can try it with wc -c < /usr/bin/wc. Moreover, POSIX utilities are guaranteed to handle binary files, unless specified otherwise explicitly.

我担心它不适用于二进制文件,但它在 Linux 和 Solaris 上都可以正常工作。您可以尝试使用wc -c < /usr/bin/wc. 此外,POSIX 实用程序保证可以处理二进制文件,除非另外明确指定。

回答by Brian Agnew

You first Perl example doesn't look unreasonable to me.

你的第一个 Perl 示例在我看来并不合理。

It's for reasons like this that I migrated from writing shell scripts (in bash/sh etc.) to writing all but the most trivial scripts in Perl. I found that I was having to launch Perl for particular requirements, and as I did that more and more, I realised that writing the scripts in Perl was probably a more powerful (in terms of the language and the wide array of libraries available via CPAN) and more efficient way to achieve what I wanted.

正是出于这样的原因,我从编写 shell 脚本(在 bash/sh 等中)迁移到使用 Perl 编写除了最琐碎的脚本之外的所有脚本。我发现我不得不针对特定需求启动 Perl,随着我越来越多地这样做,我意识到用 Perl 编写脚本可能更强大(就语言和通过CPAN提供的各种库而言)) 和更有效的方式来实现我想要的。

Note that other shell-scripting languages (e.g. python/ruby) will no doubt have similar facilities, and you may want to evaluate these for your purposes. I only discuss Perl since that's the language I use and am familiar with.

请注意,其他 shell 脚本语言(例如 python/ruby)无疑也具有类似的功能,您可能想根据自己的目的评估这些功能。我只讨论 Perl,因为这是我使用并熟悉的语言。

回答by knittl

on linux you can use du -h $FILE, does that work on solaris too?

在 linux 上你可以使用du -h $FILE,在solaris 上也可以使用吗?

回答by Paused until further notice.

If you use findfrom GNU fileutils:

如果您find从 GNU fileutils使用:

size=$( find . -maxdepth 1 -type f -name filename -printf '%s' )

Unfortunately, other implementations of findusually don't support -maxdepth, nor -printf. This is the case for e.g. Solaris and macOS find.

不幸的是, 的其他实现find通常不支持-maxdepth,也不支持-printf。例如 Solaris 和 macOS 就是这种情况find

回答by ghostdog74

if you have Perl on your Solaris, then use it. Otherwise, ls with awk is your next best bet, since you don't have stat or your find is not GNU find.

如果您的 Solaris 上有 Perl,请使用它。否则, ls 和 awk 是您的下一个最佳选择,因为您没有 stat 或者您的 find 不是 GNU find。

回答by Aditya

Did you try du -ks | awk '{print $1*1024}'. That might just work.

你有没有试过 du -ks | awk '{print $1*1024}'。那可能就行了。

回答by Aditya

Finally I decided to use ls, and bash array expansion:

最后我决定使用 ls 和 bash 数组扩展:

TEMP=( $( ls -ln FILE ) )
SIZE=${TEMP[4]}

it's not really nice, but at least it does only 1 fork+execve, and it doesn't rely on secondary programming language (perl/ruby/python/whatever)

它不是很好,但至少它只做了 1 个 fork+execve,并且它不依赖于辅助编程语言(perl/ruby/python/whatever)

回答by Martin Beckett

There is a trick in Solaris I have used, if you ask for the size of more than one file it returns just the total size with no names - so include an empty file like /dev/null as the second file:

我在 Solaris 中使用了一个技巧,如果您要求多个文件的大小,它只会返回没有名称的总大小 - 因此包含一个空文件,例如 /dev/null 作为第二个文件:

eg command fileyouwant /dev/null

例如命令文件youwant /dev/null

I can't rememebr which size command this works for ls/wc/etc - unfortunately I don't have a solaris box to test it.

我不记得哪个 size 命令适用于 ls/wc/etc - 不幸的是我没有一个solaris 框来测试它。

回答by fwhacking

I ended up writing my own program (really small) to display just the size. More information here: http://fwhacking.blogspot.com/2011/03/bfsize-print-file-size-in-bytes-and.html

我最终编写了自己的程序(非常小)来仅显示大小。更多信息:http: //fwhacking.blogspot.com/2011/03/bfsize-print-file-size-in-bytes-and.html

The two most clean ways in my opinion with common Linux tools are:

在我看来,使用常用 Linux 工具的两种最干净的方法是:

$ stat -c %s /usr/bin/stat
50000

$ wc -c < /usr/bin/wc
36912

But I just don't want to be typing parameters or pipe the output just to get a file size, so I'm using my own bfsize.

但我只是不想输入参数或管道输出只是为了获得文件大小,所以我使用我自己的 bfsize。

回答by fwhacking

Even though duusually prints disk usage and not actual data size, GNU coreutils ducan print file's "apparent size" in bytes:

即使du通常打印磁盘使用情况而不是实际数据大小,GNU coreutilsdu也可以以字节为单位打印文件的“表观大小”:

du -b FILE

But it won't work under BSD, Solaris, macOS, ...

但它在 BSD、Solaris、macOS、...