在shell中获取文件大小(以字节为单位)的可移植方式? [英] Portable way to get file size (in bytes) in shell?

查看:457
本文介绍了在shell中获取文件大小(以字节为单位)的可移植方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux上,我使用stat --format="%s" FILE,但是我可以访问的Solaris没有stat命令.那我该怎么用?

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

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

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

我已经考虑过使用:

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

甚至:

ls -nl FILE | awk '{print $5}'

但是这些都不是明智的-运行Perl只是为了获取文件大小?还是运行2个命令来执行相同的操作?

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

推荐答案

wc -c < filename(字数的缩写,-c打印字节数)是可移植的,

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

请勿忽略输入重定向.当文件作为参数传递时,文件名将在字节数之后打印.

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

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

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.

这篇关于在shell中获取文件大小(以字节为单位)的可移植方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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