How to use du to take the paths from stdin and calculate the total size? [duplicate] - linux

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get total size of folders with find & du
I can use du like this to get the file size of each file from stdin
find . -name "*.java" -exec du -h {} \;
But I can't get the total size.. Does anyone have ideas about that?
Thanks!

Answered here:
How to get total size of folders with find and du?
Use xargs(1) instead of -exec:
find . -name bak -type d | xargs du -ch
executes the command for each file found (check the find(1) documentation). Piping to xargs lets you aggregate those filenames and
only run du once.
In your case it would be:
find . -name "*.java" | xargs du -ch
Options:
-c, --total
produce a grand total

Related

How to get combined disc space of all files in a directory with help of du in linux [duplicate]

I've got a bunch of files scattered across folders in a layout, e.g.:
dir1/somefile.gif
dir1/another.mp4
dir2/video/filename.mp4
dir2/some.file
dir2/blahblah.mp4
And I need to find the total disk space used for the MP4 files only. This means it's gotta be recursive somehow.
I've looked at du and fiddling with piping things to grep but can't seem to figure out how to calculate just the MP4 files no matter where they are.
A human readable total disk space output is a must too, preferably in GB, if possible?
Any ideas? Thanks
For individual file size:
find . -name "*.mp4" -print0 | du -sh --files0-from=-
For total disk space in GB:
find . -name "*.mp4" -print0 | du -sb --files0-from=- | awk '{ total += $1} END { print total/1024/1024/1024 }'
You can simply do :
find -name "*.mp4" -exec du -b {} \; | awk 'BEGIN{total=0}{total=total+$1}END{print total}'
The -exec option of find command executes a simple command with {} as the file found by find.
du -b displays the size of the file in bytes.
The awk command initializes a variable at 0 and get the size of each file to display the total at the end of the command.
This will sum all mp4 files size in bytes:
find ./ -name "*.mp4" -printf "%s\n" | paste -sd+ | bc

Search entire server for a certain file type larger than 1GB in size? [duplicate]

This question already has answers here:
Find files with a certain extension that exceeds a certain file size
(3 answers)
Closed 3 years ago.
I have the following linux command I use to determine if a directory is larger than 1GB in size:
du -sh * | sort -hr | awk '$1 ~ /[GT]/
How would I modify this to instead search for any file that has a certain file type, such as .log filetype?
Better use find :
find . -type f -name '*.log' -size +1G
sudo find /www-data -name "*.log" -type f -size +1000000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' | sort

Find file in Linux then report the size of file searched [duplicate]

This question already has answers here:
Find files of specified size using bash in Unix [closed]
(3 answers)
Closed 9 years ago.
How can i search for file in Linux then report the size of those file i have found ?
For example, i want to search file named core.txt in my home directory in Linux, core.txt also appear on some sub-directory under my home dir. Then after found core.txt, the command should also show me file size of those files have founded.
Cheers
We can use find command to find the file and du -sh to find out its size.
We will execute du -sh on found files. So final command would be
find ~ -name "core.txt" -exec du -sh {} \;
or
find ~ -name "core.txt" | xargs du -sh
In 2nd command xargs will not handle spaces in file name. So We can tell exact delimiter to xargs to handle spaces in file name.
find ~ -name "core.txt" | xargs -d '\n' du -sh

calculate total used disk space by files older than 180 days using find

I am trying to find the total disk space used by files older than 180 days in a particular directory. This is what I'm using:
find . -mtime +180 -exec du -sh {} \;
but the above is quiet evidently giving me disk space used by every file that is found. I want only the total added disk space used by the files. Can this be done using find and exec command ?
Please note I simply don't want to use a script for this, it will be great if there could be a one liner for this. Any help is highly appreciated.
Why not this?
find /path/to/search/in -type f -mtime +180 -print0 | du -hc --files0-from - | tail -n 1
#PeterT is right. Almost all these answers invoke a command (du) for each file, which is very resource intensive and slow and unnecessary. The simplest and fastest way is this:
find . -type f -mtime +356 -printf '%s\n' | awk '{total=total+$1}END{print total/1024}'
du wouldn't summarize if you pass a list of files to it.
Instead, pipe the output to cut and let awk sum it up. So you can say:
find . -mtime +180 -exec du -ks {} \; | cut -f1 | awk '{total=total+$1}END{print total/1024}'
Note that the option -h to display the result in human-readable format has been replaced by -k which is equivalent to block size of 1K. The result is presented in MB (see total/1024 above).
Be careful not to take into account the disk usage by the directories. For example, I have a lot of files in my ~/tmp directory:
$ du -sh ~/tmp
3,7G /home/rpet/tmp
Running the first part of example posted by devnull to find the files modified in the last 24 hours, we can see that awk will sum the whole disk usage of the ~/tmp directory:
$ find ~/tmp -mtime 0 -exec du -ks {} \; | cut -f1
3849848
84
80
But there is only one file modified in that period of time, with very little disk usage:
$ find ~/tmp -mtime 0
/home/rpet/tmp
/home/rpet/tmp/kk
/home/rpet/tmp/kk/test.png
$ du -sh ~/tmp/kk
84K /home/rpet/tmp/kk
So we need to take into account only the files and exclude the directories:
$ find ~/tmp -type f -mtime 0 -exec du -ks {} \; | cut -f1 | awk '{total=total+$1}END{print total/1024}'
0.078125
You can also specify date ranges using the -newermt parameter. For example:
$ find . -type f -newermt "2014-01-01" ! -newermt "2014-06-01"
See http://www.commandlinefu.com/commands/view/8721/find-files-in-a-date-range
You can print file size with find using the -printf option, but you still need awk to sum.
For example, total size of all files older than 365 days:
find . -type f -mtime +356 -printf '%s\n' \
| awk '{a+=$1;} END {printf "%.1f GB\n", a/2**30;}'

How to find total size of all files under the ownership of a user?

I'm trying to find out the total size of all files owned by a given user.
I've tried this:
find $myfolder -user $myuser -type f -exec du -ch {} +
But this gives me an error:
missing argument to exec
and I don't know how to fix it. Can somebody can help me with this?
You just need to terminate the -exec. If you want the totals for each directory
possibly -type d is required.
find $myfolder -user $myuser -type d -exec du -ch {} \;
Use:
find $myfolder -user gisi -type f -print0 | xargs -0 du -sh
where user gisi is my cat ;)
Note the option -s for summarize
Further note that I'm using find ... -print0 which on the one hand separates filenames by 0 bytes, which are one of the few characters which are not allowed in filenames, and on the other hand xargs -0 which uses the 0 byte as the delimiter. This makes sure that even exotic filenames won't be a problem.
some version of find command does not like "+" for termination of find command
use "\;" instead of "+"

Resources