我想运行一个 find
命令来查找特定的文件列表,然后遍历该文件列表以运行一些操作。我还想找出该列表中所有文件的总大小。
我想先列出文件,然后再做其他操作。有没有一种简单的方法可以只报告列表中所有文件的总大小?
本质上,我试图在下面的代码片段中为“total_size”变量找到一个单行代码:
#!/bin/bash
loc_to_look='/foo/bar/location'
file_list=$(find $loc_to_look -type f -name "*.dat" -size +100M)
total_size=???
echo 'total size of all files is: '$total_size
for file in $file_list; do
# do a bunch of operations
done
最佳答案
您应该能够将 $file_list
传递给 du
:
du -ch $file_list | tail -1 | cut -f 1
du
选项:
-c
显示总数-h
人类可读(即 17M)
du
将为每个文件打印一个条目,然后是总数(使用 -c
),因此我们使用 tail -1
来仅修剪到最后一行,然后 cut -f 1
将该行修剪到仅第一列。
关于bash - 获取 UNIX 中文件列表的总大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21989860/