You are not logged in.

#1 2010-09-01 18:16:53

koral
Member
Registered: 2010-08-05
Posts: 26

Display directories' size in ls

Hi,

Is there any way to make the "ls" command display the size of directories ? I realize I always need to do an additionnal "du -hs *" to get that information, and as this is a very simple thing, I'm wondering why it's not included in "ls" command...

Actually, given the filesystem implementation, I guess the "du" command has to go through each subfile to compute the whole size, which makes it last quite long for big directories; I would then understand that we don't want it to be included in "ls" command... but still, I feel very strange about not being able to get such a simple piece of information quickly, so I prefer asking smile .

Offline

#2 2010-09-01 18:29:05

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Display directories' size in ls

You can get that info quickly if you alias your commands. It's also simple.
I know of no way to do it with ls.

Offline

#3 2010-09-01 18:29:37

xenofungus
Member
From: Spain
Registered: 2009-10-17
Posts: 63

Re: Display directories' size in ls

try du -ah --max-depth 1

Offline

#4 2010-09-04 12:51:55

koral
Member
Registered: 2010-08-05
Posts: 26

Re: Display directories' size in ls

@xenofungus: your command doesn't work; anyway, it's not what I'm looking for, since I said I wanted this information included in "ls" output.

Last edited by koral (2010-09-04 14:02:10)

Offline

#5 2010-09-04 12:55:50

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Display directories' size in ls

koral wrote:

@xenofungus: your command doesn't work; anyway, it's not what I4m looking for, since I said I wanted this information included in "ls" output.

It does work <shrugs>

alias ls="du -ah --max-depth 1"

Offline

#6 2010-09-04 13:10:20

xenofungus
Member
From: Spain
Registered: 2009-10-17
Posts: 63

Re: Display directories' size in ls

I'd use a different name unless you don't want to use ls anymore / make it's usage more complicated

Offline

#7 2010-09-04 13:15:44

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Display directories' size in ls

xenofungus wrote:

I'd use a different name unless you don't want to use ls anymore / make it's usage more complicated

koral wrote:

I said I wanted this information included in "ls" output

alias lss="ls && du -h --max-depth 1"

Offline

#8 2010-09-04 14:01:45

koral
Member
Registered: 2010-08-05
Posts: 26

Re: Display directories' size in ls

Indeed, it works. Actually, I already had an alias for "du" command which added arguments that weren't compatible with yours.

However, there are 2 problems with this solution:
* the output is a concatenation of, first, a "ls" output, and then, a "du" output; not really convenient to read; the ideal would be to display "du" 's output as an additionnal column of "ls" 's output; I could write a wrapper which would do the job, but there would still be next problem:
* "du" command is quite slow; isn't there a way to get its result instantly ? I mean: we could store incrementally the sizes of directories, so that there wouldn't be any long computation to get this piece of information; is there anything like this implemented in the filesystem ?

Anyway, thanks for your answers up to now smile .

Offline

#9 2010-09-04 14:26:30

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Display directories' size in ls

Have a look a ncdu (it's in [community]). ncdu means ncurses du, it's a simple tool that IIRC caches the sizes, so if you don't move too much data around it should fast enough.

Edit: you can set up a cron job to run ncdu on some dir or even on / e.g. every ten minutes so you get the updated view if / when you need it in about 2 seconds.

Last edited by karol (2010-09-05 06:04:49)

Offline

Board footer

Powered by FluxBB