Get total size of a list of files in UNIX
Methods explained here have hidden bug. When file list is long, then it exceeds limit of shell comand size. Better use this one using du:
find <some_directories> <filters> -print0 | du <options> --files0-from=- --total -s|tail -1
find produces null ended file list, du takes it from stdin and counts. this is independent of shell command size limit. Of course, you can add to du some switches to get logical file size, because by default du told you how physical much space files will take.
But I think it is not question for programmers, but for unix admins :) then for stackoverflow this is out of topic.
This code adds up all the bytes from the trusty ls for all files (it excludes all directories... apparently they're 8kb per folder/directory)
cd /; find -type f -exec ls -s \; | awk '{sum+=$1;} END {print sum/1000;}'
Note: Execute as root. Result in megabytes.
You should simply be able to pass $file_list
to du
:
du -ch $file_list | tail -1 | cut -f 1
du
options:
-c
display a total-h
human readable (i.e. 17M)
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.
The problem with du
is that it adds up the size of the directory nodes as well. It is an issue when you want to sum up only the file sizes. (Btw., I feel strange that du
has no option for ignoring the directories.)
In order to add the size of files under the current directory (recursively), I use the following command:
ls -laUR | grep -e "^\-" | tr -s " " | cut -d " " -f5 | awk '{sum+=$1} END {print sum}'
How it works: it lists all the files recursively ("R"
), including the hidden files ("a"
) showing their file size ("l"
) and without ordering them ("U"
). (This can be a thing when you have many files in the directories.) Then, we keep only the lines that start with "-" (these are the regular files, so we ignore directories and other stuffs). Then we merge the subsequent spaces into one so that the lines of the tabular aligned output of ls
becomes a single-space-separated list of fields in each line. Then we cut
the 5th field of each line, which stores the file size. The awk
script sums these values up into the sum
variable and prints the results.