using tree or du to display directory list with sizes
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Note that, in the above, the first directory, the one with no date as part of its name, is at the top but it's subs are far below. I find that, if I add even a phony date to the end, something like 0-00-00, it sorts correctly:
hmm. that seems like it would add a layer of complexity i'd like to avoid. i was hoping to just use the above commands to direct out to a txt file and be done. this seems like i'd have to run the ncdu app, then figure out how to sort correctly, then export to txt file. is that right? is there no way to get du or tree, or a combination of the two to do this in a line?
edit:
just looked into ncdu and it doesn't easily seem to do what i'm looking for. what i want is a list of ONLY directories, each followed by its subdirectories. something like this:
I've been having a problem with sorting that is beginning to look like more of a system-wide issue than a specific command issue.
I'm trying to print a list of directories and their sub-directories. It's working well until I get to some directories with specific naming conventions.
Using some command line options I can print a list that, generally, is fine. Some directories, however list like this:
Note that, in the above, the first directory, the one with no date as part of its name, is at the top but it's subs are at the bottom. I find that, if I add even a phony date to the end, something like 0-00-00, it sorts correctly:
I've been trying to find options within the commands I've been using (tree and du) to try to address this issue. I've just found, however, that this seems to be a more general problem, hence, my coming to this, more general, forum.
I imported my list into LibreOffice Calc and tried sorting it there. To my surprise, it sorted the same way my command line options had sorted the list, with the non-dated sub-directories separated from the parent by dated directories.
Is there no way around this? (If it makes a difference, I'm using Kubuntu 20.04.)
The system sorts in ascii sequence as do most apps.
Since the # (ascii 35) sign comes after the space " " (ascii 32) it always will sort as you are seeing. You can see the ascii sequence online with a quick search for 'ascii chart'.
If your directory names did not have the space in the name, then the # (ascii 35) would sort before the first number. The numbers are "0" (ascii 48) thru "9" (ascii 57). Even replacing the space with a hyphen (ascii 45) or an underscore (ascii 95) would make it sort the way you expect.
Even the directory separator (/) ascii 47 comes after the space which also puts those subdirectories after the names containing the space.
(Come to think of it, the hyphen would still sort before the "/" so the underscore would be the better choice to replace the spaces.)
I see the same thing when sorting numbers with single digits and 2 or 3 digit numbers. As an example, in most cases the number 2 will be sorted between 19 and 20 but 02 will be sorted properly with other 2 digit numbers.
Last edited by computersavvy; 02-11-2022 at 10:16 PM.
from what you say, it's sounding like the only way i can get around this is by renaming my directories. is that right? there's no switch or something i can apply to my searching and sorting?
edit:
i suppose what's surprising to me is that the du command, which i used to produce the lists above:
Code:
du -h ./* | sort -k2
seems to be constructing the string for each directory or sub-directory listing and then using that full string for its sort, rather than the actual directory structure. if it ordered by structure it would sort properly. since it seems to be looking at the string for each listing, it sees things like the "/" separator and sorts using that, causing the ascii ordering you describe. hmmm.
end edit
'du -S .| sort -k 2' will give you a list of directories and their contents size, but it won't include in the totals the contents of any subdirectories: only the size of files in that directory itself.
find sorts its output by directory
using '*' for spec to find means you won't get the current directory and leading './' in its output.
-print0 (find) and -0 (xargs) allow spaces and other special characters in filenames to be handled gracefully.
xargs -n 1 runs du on each directory individually -r just ignores the parameter if it's empty
@pan64:
interesting. i don't quite follow how to use that to get where i want but it seems to have potential.
@ondoho:
yes. i started out looking at this as more specific to certain commands but found the issue to be more general. i'm mainly working on it here now.
@goumba:
also interesting. i tried this command but it produced a lot of duplicates. sorting of longer entries was out of order too. nice to get rid of the ./ though!
i'm thinking that my original command along with @pan64's -t delimiter option might be the ticket. hope to find examples.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.