3

I started a few weeks with Linux bash programming, but I'm struggling with a problem: How can I get the size of multiple directories (path saved on a .txt file) and then, write into another file, the directory path, followed by size and the total size occupied by that directories. I already tried this, but I'm not sure:

totsize=0
while read -r diretory; do
   size=$(du -ch "$directory")
   echo "$diretory size: $size" > "$homedir/$filename2"
   $totsize += $(du -c "$homedir/$diretorio")
done < "$homedir/diretories.txt"
sudo echo "total size: $totsize" >> "$homedir/$filename2"

Thanks in advance (and sorry for bad english)

user5445222
  • 33
  • 1
  • 5

3 Answers3

3

When dealing with files or directories by their name, I highly recommend to deal with them separated by the nullbyte character, not by a newline. Because officially a filename can contain a newline. So you should not make a list of the directories separated by a newline.

Anyway, when you have this list you can just use this:

du -sch --files0-from=dirs.txt
  • -s is like -d0 and will only summarize a total for each argument.
  • -c calculates a grand total.
  • -h does that all in human readable format.
  • --files0-from=dirs.txt reads the filenames/directorynames from the given file terminated by nullbyte.

With your list, it whould look like (but with the pitfall of filenames with newlines in it):

tr '\n' '\0' <dirs.txt | du -sch --files0-from=-
chaos
  • 28,186
1

You can get size of directories with du command like this:

du -h -d 1 "path to parent directory"

-d: the depth of subdirectories

1

Use find combined with du and redirect to file with >

sudo find /path/to/topDir -maxdepth 1 -type d  -exec du -sh {} \;  > output.txt

Example:

xieerqi:$ sudo find Desktop -maxdepth 1 -type d  -exec du -sh {} \; > outputFile1.txt                                                            
[sudo] password for xieerqi: 

xieerqi:$ cat outputFile1.txt                                                                                                                    
2.7G    Desktop
69M Desktop/linux-kernel-4.1.0
1.1M    Desktop/DOCS
6.8M    Desktop/The comments that became a reporter’s death sentence | New York Post_files
4.0K    Desktop/newdir
6.7M    Desktop/IMAGES
12M Desktop/TIRES
504K    Desktop/MSU-TEMPLATES
341M    Desktop/PDFS
19M Desktop/java

To have this command give disk usage for specific directories from list , what you can do is combine cat and xargs with the command above. For example,

 cat dirList.txt | xargs -I dir sudo find dir -maxdepth 0 -type d  -exec du -sh {} \; 

Some directories of which you're not an owner require sudo access to list their disk usage, hence combine that with sudo sh -c. Let me demonstrate,

xieerqi:$ sudo sh -c "cat dirList.txt | xargs -I dir find dir -maxdepth 0 -type d  -exec du -sh {} \+"                                           
[sudo] password for xieerqi: 
2.7G    /home/xieerqi/Desktop
11M /bin
5.5G    /usr

xieerqi:$ cat dirList.txt                                                                                                                        
/home/xieerqi/Desktop
/bin
/usr