Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get total size of a list of files in UNIX

Tags:

bash

shell

unix

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 
like image 623
HotDogCannon Avatar asked Feb 24 '14 13:02

HotDogCannon


People also ask

How do I get the size of a file list in Linux?

To get the total size of a directory in Linux, you can use the du (disk-usage) command.

How do I check the size of a file in Unix?

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.


2 Answers

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.

like image 61
sanmiguel Avatar answered Oct 04 '22 16:10

sanmiguel


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.

like image 25
Znik Avatar answered Oct 04 '22 18:10

Znik