获取 UNIX 中文件列表的总大小 [英] Get total size of a list of files in UNIX
问题描述
我想运行一个 find
命令,该命令将查找某个文件列表,然后遍历该文件列表以运行一些操作.我还想找到该列表中所有文件的总大小.
I want to run a find
command that will find a certain list of files and then iterate through that list of files to run some operations. I also want to find the total size of all the files in that list.
我想首先制作文件列表,然后进行其他操作.有没有一种简单的方法可以只报告列表中所有文件的总大小?
I'd like to make the list of files FIRST, then do the other operations. Is there an easy way I can report just the total size of all the files in the list?
本质上,我试图在下面的代码片段中为total_size"变量找到一个单行:
In essence I am trying to find a one-liner for the 'total_size' variable in the code snippet below:
#!/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
:
You should simply be able to pass $file_list
to du
:
du -ch $file_list | tail -1 | cut -f 1
du
选项:
-c
显示总数-h
人类可读(即 17M)
-c
display a total-h
human readable (i.e. 17M)
du
将为每个文件打印一个条目,然后是总数(使用 -c
),因此我们使用 tail -1
只修剪到最后一行,cut -f 1
只将该行修剪到第一列.
du
will print an entry for each file, followed by the total (with -c
), so we use tail -1
to trim to only the last line and cut -f 1
to trim that line to only the first column.
这篇关于获取 UNIX 中文件列表的总大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!