Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to do this is with an associative array. An associative array maps a series of keys to values, for example:</p> <pre><code>directory1 -&gt; 10 GB directory2 -&gt; 12 MB directory3 -&gt; 40 KB </code></pre> <p>The keys in an associative array must be unique. That's great! The paths to our directories are also unique. Let's put them in an associative array. I will show how to do this in <code>awk</code> but plenty of other languages have associative arrays (like Perl, which calls them hashes).</p> <pre><code>du | awk '{ val = $1; dir = $2; sizes[dir] = val }' </code></pre> <p>(I took out the arguments you pass to <code>du</code> for simplicity)</p> <p>What does this do? <code>awk</code> reads the output of <code>du</code> line by line; for each line, it adds an element to the associative array <code>sizes</code> with the directory name as the index and the size as the value. If our original input looked like this</p> <pre><code>40GB folder1/subfolder1 15GB folder1/subfolder2 10GB folder2/subfolder1 </code></pre> <p>our array would look like this:</p> <pre><code>sizes[folder1/subfolder1] -&gt; 40GB sizes[folder1/subfolder2] -&gt; 15GB sizes[folder2/subfolder1] -&gt; 10GB </code></pre> <p>But in our final output we just want to see values for the subdirectories. <code>awk</code> has functions for string manipulation, so let's tweak our code to strip off leading directories:</p> <pre><code>du | awk '{ val = $1; dir = $2; sub(/^.*\//, "", dir); sizes[dir] = val }' </code></pre> <p>The <code>sub</code> function strips off everything from the last <code>/</code> to the beginning of the path. Now our array looks like this:</p> <pre><code>sizes[subfolder2] -&gt; 15GB sizes[subfolder1] -&gt; 10GB </code></pre> <p>Great! Now we only have values for the subdirectories. There's just one little problem. The values aren't totals. Since we had more than one subdirectory named <code>subfolder1</code>, we overwrote the first value (40GB) with the second one (10GB). When we run into an index that already exists in our array, what we really want to do is add its value to the existing value:</p> <pre><code>du | awk '{ val = $1; dir = $2; sub(/^.*\//, "", dir); sizes[dir] += val }' </code></pre> <p>(I changed <code>sizes[dir] = val</code>, which uses assignment, to <code>sizes[dir] += val</code>, which adds <code>val</code> to whatever is already in <code>sizes[dir]</code>)</p> <p><code>awk</code> magically takes care of some things for us, like converting 15GB to the number 15. Now our array looks like this:</p> <pre><code>sizes[subfolder2] -&gt; 15 sizes[subfolder1] -&gt; 50 </code></pre> <p>which shows us the totals we're looking for. Now, how do we display this? We can loop through the array and print out the keys and values like this:</p> <pre><code>du | awk '{ val = $1; dir = $2; sub(/^.*\//, "", dir); sizes[dir] += val } \ END { for (dir in sizes) print dir, sizes[dir], "GB" }' </code></pre> <p>and our results are</p> <pre><code>subfolder1 50 GB subfolder2 15 GB </code></pre> <hr> <p><strong>EDIT:</strong> Here are the results I get using the <code>du</code> output in the updated question.</p> <pre><code>nr106_0060 2 GB nr106_0050 3 GB nr106_0045 2 GB nr106_0040 2 GB nr106_0035 2 GB nr106_0030 2 GB nr106_0020 2 GB nr106_0010 2 GB </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload