GNU bug report logs - #28657
Random sort-order in du

Previous Next

Package: coreutils;

Reported by: Holger Klene <h.klene <at> gmx.de>

Date: Sat, 30 Sep 2017 20:47:02 UTC

Severity: normal

Tags: notabug

Done: Assaf Gordon <assafgordon <at> gmail.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Assaf Gordon <assafgordon <at> gmail.com>
To: Holger Klene <h.klene <at> gmx.de>, 28657 <at> debbugs.gnu.org
Subject: bug#28657: Random sort-order in du
Date: Sat, 30 Sep 2017 15:08:35 -0600
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












This bug report was last modified 7 years and 231 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.