在 linux 上查看文件大小

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

watch file size on linux

linuxshellcommand-linewatch

提问by texai

I want to watch the growing size of a single file, so i use this command:

我想观察单个文件不断增长的大小,因此我使用以下命令:

texai@maelstrom ~$ ls -lh club_prod.sql | awk '{print }'
116M

Now I want to watch that result each 5 seconds so:

现在我想每 5 秒看一次结果,所以:

texai@maelstrom ~$ watch -n 5 ls -lh club_prod.sql | awk '{print }'

but this command doesn't return any result

但此命令不返回任何结果

采纳答案by larsks

You're piping the output of watchinto awk. If you simplify your command line, what you have is:

您正在将 的输出通过管道传输watchawk. 如果你简化你的命令行,你所拥有的是:

 watch <some arguments> | awk '{print }'

That's not what you want. Try:

那不是你想要的。尝试:

watch -n 5 "ls -lh club_prod.sql | awk '{print $5}'"

回答by myki

watch -n 5 "ls -lh club_prod.sql | awk '{print $5}'"

回答by geekosaur

You need to quote the pipeline so that it is done withinwatch.

您需要引用管道,以便watch.

watch -n 5 "ls -lh club_prod.sql | awk '{print $5}'"

Note also the \added to \$5because the outer quotes are now double quotes, in which $-variables are expanded. (Other methods of quoting are generally uglier than this.)

还要注意\添加到\$5因为外部引号现在是双引号,其中$-variables 被扩展。(其他引用方法通常比这更丑。)

回答by tamasgal

watch -n 5 "du -h club_prod.sql"

回答by JacaHyman

Not exactly related, but if you want to monitor growth rate of some file, you could use following command:

不完全相关,但如果您想监控某个文件的增长率,您可以使用以下命令:

tail -f yourfile.txt | pv > /dev/null

tail -f yourfile.txt | pv > /dev/null

  • tail -f- outputs data appended to file
  • pv- measures data flow through pipe
  • > /dev/null- standard output gets discarded
  • tail -f- 输出附加到文件的数据
  • pv- 测量通过管道的数据流
  • > /dev/null- 标准输出被丢弃

Note: sometimes pvmay be not preinstalled

注意:有时pv可能没有预装

I hope this will help somebody :)

我希望这会帮助某人:)

回答by Osiris

For fast detailed growth watching of a file, every 0.1 second:

对于文件的快速详细增长观察,每 0.1 秒:

watch -n 0.1 "ls -l /mnt/some/file | awk '{print $5}' | sed -re ' :rep ; s/([0-9])([0-9]{3})($|[^0-9])/,/ ; t rep '"

This will produce something like 62,673,539,072.

这将产生类似 62,673,539,072 的结果。

回答by Punja Solanki

You can perform this like that:

你可以这样执行:

while true; do
  du -s **file_or_directory**
 sleep **time_interval**
done

回答by kvantour

The usage of watchis correct, but the usage of lsI would avoid. I would recommend the usage of stator du, but this depends on what you want.

的用法watch是正确的,但ls我会避免使用。我会推荐使用stator du,但这取决于你想要什么。

  • du:If you want the space occupied on your drive
  • stat:If you want the number of bytes your file contains (how many bytes can I read from the file)
  • du:如果你想占用你的驱动器上的空间
  • stat如果您想要文件包含的字节数(我可以从文件中读取多少字节)

Imagine working with a compressed file system, or with processing sparse files, internal fragmentation, indirect blocks ...

想象一下使用压缩文件系统,或者处理稀疏文件、内部碎片、间接块......

For both cases, the result would be:

对于这两种情况,结果都是:

$ watch -n 5 'stat --printf "%s\n" file'
$ watch -n 5 'du -B1 file'

Both results can actually be obtained in a single command with stat:

这两个结果实际上都可以在单个命令中获得stat

$ watch -n 5 'stat --printf "%s %b %B\n" file'

The product of the last two columns is the result of du.

最后两列的乘积是 的结果du

回答by Marcelo Pacheco

#!/bin/bash  
# Watch File Size and Growth  
# Author: Marcelo Pacheco - [email protected]  
# Syntax: watchfilesize filetomonitor
nm=""  
while true  
do  
  sz=$(stat -c %s "$nm")  
  sleep 1m  
  sz1=$(stat -c %s "$nm")  
  echo Growth: $(((sz1-sz)/1024))KB/min Size: $((sz1/1024/1024))MB  
  sz=$sz1  
done