-1

The output of du -sh * | sort -h lists all the files and directories in sorted order based on size. But the problem is; the output doesn't differentiate the directories from files.

For example:

15K file1backup
16K Desktop

Is there any option to du which makes it easier to differentiate the files and directories?

bprasanna
  • 99
  • 3
  • I'm afraid this is really a RTFM question. See answer below. – Unbeliever Oct 04 '16 at 18:04
  • @Unbeliever Thank you, since i couldn't figure out exact options which will differentiate the directories from files in man page i thought of asking here. If you feel irritated by this question sorry for that. – bprasanna Oct 05 '16 at 01:53

4 Answers4

1

The nearest I could get using the original command from OP was this:

 ls -p | xargs -I{} du {} -sh | sort -h

..which now seems to work.

John K. N.
  • 2,055
  • 1
  • 17
  • 28
  • 1
    You don't need `-I{}` if you use `xargs du -sh` (and let the variable args go at the end by default), but you need to add `-d'\n'` if any filenames contain whitespace (except newline), quotes or backslash. – dave_thompson_085 Oct 04 '16 at 10:48
0

No, my reading of the manual says that du does not differentiate between files and directories.


You cold differentiate yourself with something like

find  . -maxdepth 1 -type f -print0 | du -sh --files0-from -

for files and

 find  . -maxdepth 1 -type d -print0 | du -sh --files0-from -

for directories. Adjust other parameters as required.

user9517
  • 115,471
  • 20
  • 215
  • 297
0

Does man du offer a switch to do so? - No.

Can you be clever about it? Maybe.

du -s */ |sort -n will restrict you to subdirectories, excluding the files in your $PWD and you can use ls to sort those files by size quite easily with ls --group-directories-first -Ssh.

Or use a different tool altogether: ncdu

HBruijn
  • 77,029
  • 24
  • 135
  • 201
0

You really should try the manual. Simply doing du -h will only do directories you actually have to add an argument to get it to do files as well (-a). The reason your command does files is because of the *.

If you only want top level directories sorted, probably the simplest is:

du -h | grep -v '[^.]/' | sort -h
Unbeliever
  • 2,336
  • 1
  • 10
  • 19