[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#28657: Random sort-order in du
From: |
Assaf Gordon |
Subject: |
bug#28657: Random sort-order in du |
Date: |
Sat, 30 Sep 2017 15:08:35 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 |
Hello,
On 2017-09-30 02:45 PM, Holger Klene wrote:
>
> du -hd1 /media/<USER>/backintime/<HOST>/<USER>/1/
[...]
> Now du returns something like:
> 1G 2016
> 0G 2015
> 0G 2017
> 1G .
>
> [...]
>
> But now the order determines, which folder the size is reported against. But
> I had to learn, that
> the order of items in the filesystem is unpredictable:
>
[...]
> The desired behavior in this case would be either alphabetical or
> reverse-alphabetical to
> attribute the FileA to the first or last appearance in the tree respectively.
'du' can report the directories in the order they are given on the
command line, and so you can use a slightly longer command to force
directory order.
Example 1:
Create a tiny directory structure example:
mkdir -p data/{a,b,c}
seq 10000 > a/1
ln a/1 b/2
ln a/1 c/3
Default output is unordered, as you've observed:
$ du -hd1 data
580K data/c
4.0K data/b
4.0K data/a
592K data
But if you use shell-globbing, the shell will automatically
order the directory alphabetically:
$ du -c -hd1 data/*
580K data/a
4.0K data/b
4.0K data/c
588K total
If you want a more complicated ordered (e.g. reverse order),
you can combine "find" (to list the directories), "sort" (to sort in
your desired order) and "du --files0-from" to read the file list from stdin:
$ find data -maxdepth 1 -mindepth 1 -type d -print0 \
| sort -z -k1r,1 \
| du --files0-from=- -c -h
580K data/c
4.0K data/b
4.0K data/a
588K total
Note the "find -print0" and "sort -z" which force NUL-terminated
filenames (instead of newlines) - this is required to be used with "du
--files0-from" .
Another tip:
If you have filenames with mixed letters and numbers or numbers with
different width (e.g. "2017.7" and "2017.11") you can use
"sort -z -k1V,1" to sort them correctly.
Hope this helps,
- assaf