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?
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
To get the total size of a directory in Linux, you can use the du (disk-usage) command.
don't worry we have a got a UNIX command to do that for you and command is "df" which displays the size of the file system in UNIX. You can run "df" UNIX command with the current directory or any specified directory.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With