我有一个文件,其中包含我想知道文件总大小的文件列表。有命令可以这样做吗?
我的操作系统是一个非常基本的 linux (Qnap TS-410)。
编辑:
文件中的几行:
/share/archive/Bailey Test/BD006/0.tga
/share/archive/Bailey/BD007/1 version 1.tga
/share/archive/Bailey 2/BD007/example.tga
Mat*_*erg 15
我相信这样的事情会在busybox中工作:
du `cat filelist.txt` | awk '{i+=$1} END {print i}'
Run Code Online (Sandbox Code Playgroud)
我没有和你一样的环境,但是如果你遇到文件名中的空格问题,这样的事情也可以:
cat filelist.txt | while read file;do
du "$file"
done | awk '{i+=$1} END {print i}'
Run Code Online (Sandbox Code Playgroud)
编辑 1:
@stew 就在他下面的帖子中,du 显示了磁盘使用情况,而不是确切的文件大小。要更改行为,busybox 使用 -a 标志,因此请尝试:du -a "$file"
获取精确的文件大小并比较输出/行为。
小智 8
du -c `cat filelist.txt` | tail -1 | cut -f 1
Run Code Online (Sandbox Code Playgroud)
-c
添加行“总大小”;
tail -1
取最后一行(总大小);
cut -f 1
删掉“总”这个词。
我不知道您的 linux 工具是否能够做到这一点,但是:
cat /tmp/filelist.txt |xargs -d \\n du -c
Run Code Online (Sandbox Code Playgroud)
做, xargs 会将分隔符设置为换行符,而 du 将为您生成总计。
查看http://busybox.net/downloads/BusyBox.html似乎“busybox du”将支持总计选项,但“busybox xargs”将不支持自定义分隔符。
同样,我不确定您的工具集。
while read filename ; do stat -c '%s' $filename ; done < filelist.txt | awk '{total+=$1} END {print total}'
Run Code Online (Sandbox Code Playgroud)
这类似于 Mattias Ahnberg 的解决方案。使用“读取”可以解决带有空格的文件名/目录的问题。我使用stat
而不是du
获取文件大小。du 正在获取它在磁盘上使用的空间量而不是文件大小,这可能会有所不同。根据您的文件系统,一个 1 字节的文件仍将在磁盘上占用 4k(或任何块大小)。所以对于 1 字节的文件,stat 表示 1 个字节,du 表示 4k。
小智 5
这是该问题的另一种解决方案:
cat filelist.txt | tr '\n' '\0' | wc -c --files0-from=-
Run Code Online (Sandbox Code Playgroud)