Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

total size of group of files selected with 'find'

For instance, I have a large filesystem that is filling up faster than I expected. So I look for what's being added:

find /rapidly_shrinking_drive/ -type f -mtime -1 -ls | less 

And I find, well, lots of stuff. Thousands of files of six-seven types. I can single out a type and count them:

find /rapidly_shrinking_drive/ -name "*offender1*" -mtime -1 -ls | wc -l 

but what I'd really like is to be able to get the total size on disk of these files:

find /rapidly_shrinking_drive/ -name "*offender1*" -mtime -1 | howmuchspace 

I'm open to a Perl one-liner for this, if someone's got one, but I'm not going to use any solution that involves a multi-line script, or File::Find.

like image 482
Ed Hyer Avatar asked Jul 15 '09 21:07

Ed Hyer


People also ask

How do you find the total file size?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

How do you list the files depending on the file size?

To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size). You can output the file sizes in human-readable format by adding the -h option as shown. And to sort in reverse order, add the -r flag as follows.

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

ls -l | tr -s ' ' | cut -d ' ' -f <field number> is something I use a lot. The 5th field is the size. Put that command in a for loop and add the size to an accumulator and you'll get the total size of all the files in a directory.

How do you sort LS by size?

To sort files by size, use the option -S with the ls command. Mind it, it's capital S for sorting. That's good but you can make it better by adding the -h option. This option makes the output of the ls command displays the file size in human readable formats.


2 Answers

The command du tells you about disk usage. Example usage for your specific case:

find rapidly_shrinking_drive/ -name "offender1" -mtime -1 -print0 | du --files0-from=- -hc | tail -n1 

(Previously I wrote du -hs, but on my machine that appears to disregard find's input and instead summarises the size of the cwd.)

like image 142
Stephan202 Avatar answered Oct 04 '22 00:10

Stephan202


Darn, Stephan202 is right. I didn't think about du -s (summarize), so instead I used awk:

find rapidly_shrinking_drive/ -name "offender1" -mtime -1 | du | awk '{total+=$1} END{print total}' 

I like the other answer better though, and it's almost certainly more efficient.

like image 33
pix0r Avatar answered Oct 04 '22 00:10

pix0r