Can you du only certain files?
I have a list of files in a text document & I'd like to find out how much space all of those files use.
I was just going through the list with a shell script & accumulating their file size, but I run into a 2GB limit with the shell script. I know I could rewrite the script in python or perl, but before I go reinventing the wheel, is there some way to du only those files? the man page for du says I can: --files0-from=F summarize disk usage of the NUL-terminated file names specified in file F but it doesn't seem to work for more than one file in the given text doc. Is there some special way to null-terminate the file names in the text doc? I've tried one file per line, enclosing file names in quotes, adding '\0' at the end of the file name... nothing seems to work. |
If "list" is the file with the names, you can simply do:
du `cat list` (Those are backticks, not single quotes) It's not clear from the man page, but du will accept several file names---not sure if there is an upper limit, but worth a try. In my quick test, it did not matter if the filenames in "list" were delimited by spaces or newlines. |
Maybe this can give You a clue? Given that Your filelist has newline separated filenames (and including those pesty spaces in filenames, replace \n with \0 using tr and tell du to use stdin. Some creative use of cut could summarize the first column for You as well (it is tab-separated).
Quote:
PS Just thought of something, if Your filelist is very long You may have to use xargs because the list of arguments grow to big. It often happens when trying to operate on entire directory trees, with tar for example. |
The problem with both of these methods is that they get the disk usage of each file individually... I want a summary of all files... i.e. "du -sh <list of files>" - but then only get one entry for output which is the disk usage of all of the files in the list.
|
Quote:
Quote:
PS Maybe You meant that du itself could do the summarisation but I tried it and couldn't get it to work, maybe it can't do that with files from list... |
Quote:
|
Quote:
I've since re-written the script in python, but I would sure like to know if it's possible to get a summary of disk usage of a list of files with du |
Someone already said to run du on the list and then add up the numbers. Beyond that, the only way to get du to do it is to put them all in a folder.
du <foldername> gives the total of everything in the folder. |
This looks like a classic use of awk:
du *.mp3 | awk '{ sum+=$1 } END {print sum}' |
Quote:
Code:
du -sL foldername |
Quote:
Quote:
|
Quote:
I'll just add that the reason I was looking to du in the first place is that summing up file sizes in a shell script has a 2GB limit & almost all of my searches are greater than 2GB. Well... 2GB with tcsh, don't know about bash (and yes I know the pros and cons of bash vs tcsh). That is a pretty cool one-liner, though. ;) regardless, I'm solved with awk. Thanks all. |
All times are GMT -5. The time now is 02:26 PM. |