How to list recursive file sizes of files and directories in a directory?

203,165

Solution 1

I guess the easiest way is by typing ls -l, or ls -lh which will provide the file size in human-readable format (KB, MB, etc).

If 'recursively' means listing all the subsequent folders, e.g.:

/foo/
/foo/bar/ ....

Then you should also add parameter R, like ls -lR or ls -lhR

More information for ls can be found by typing man ls

Update:

The following command as Lekensteyn proposed will probably do the job:

du -h --max-depth=1 <folder>

-h is for human-readable
--apparent-size is another way to display sizes as already stated
--max-depth is the level of subfolders you want to go down to.

Solution 2

apt-get install ncdu

enter image description here

It is interactive too so if you want to check on a sub folder just UP, DOWN, and Enter to it.

Solution 3

To get the total size of a directory and all children

du -hs directory/*

Solution 4

Also check out tree. It is not installed by default but is the repositories.

Example:

richard@legend:~$ tree Applications/ -s
Applications/
├── [           4096]  AlexFTPS-1.0.2
│   ├── [      31232]  AlexPilotti.FTPS.Client.dll
│   ├── [     274432]  C5.dll
│   ├── [       1457]  C5-License
│   ├── [      35147]  COPYING
│   ├── [       7639]  COPYING.LESSER
│   ├── [         70]  ftps
│   ├── [      28672]  ftps.exe
│   ├── [      98304]  Plossum CommandLine.dll
│   ├── [       1557]  Plossum-License
│   └── [       2560]  README
└── [           4096]  src
    └── [     180849]  AlexFTPS_bin_1.0.2.zip

More options can be found in the man page.

Solution 5

Since you don't specifically mention you need a terminal-based solution, I think baobab a.k.a. Disk Usage Analyzer is missing from the list.

It is installed in Ubuntu by default and does exactly what you want in a nice graphical UI with the ability to drill down the directory hierarchy.

Apart from displaying a list of directories with their sizes, it is also showing a rings or treemap chart of filesystem usage, which is extremely useful for visualising the directories which take up the most space.

baobab the Disk Usage Analyzer

Share:
203,165

Related videos on Youtube

ndg
Author by

ndg

Updated on September 18, 2022

Comments

  • ndg
    ndg almost 2 years

    How do I list all the files in a directory and their recursive file sizes?

    ---edit

    I want to see the sizes 'rolled up' to the parent directories in the directory listed. I don't want to see the child directories or their contents or sizes.

    • Pavlos G.
      Pavlos G. almost 13 years
      You mean that you want to see the total size (sum) of all the files in a folder, like right-clicking on a folder on your desktop and selecting 'properties' ?
    • Lekensteyn
      Lekensteyn almost 13 years
      Are you looking for software which helps you to find big files, or something to get a (collapsable) file tree?
    • ndg
      ndg almost 13 years
      Hi @Pavlos G - I mean the sum for each of the directories in the directory I'm currently in - not the sum for the overall directory.
    • Pavlos G.
      Pavlos G. almost 13 years
      @J G - Check my update ;-)
  • ndg
    ndg almost 13 years
    Should sizes be shown?
  • Richard Holloway
    Richard Holloway almost 13 years
    J G: The -s shows the sizes.
  • ndg
    ndg almost 13 years
    I'd like the next level down from that - the recursive sizes of all the first level children.
  • Richard Holloway
    Richard Holloway almost 13 years
    I'll edit my answer. You need to use du -h directory/*
  • Steven Lu
    Steven Lu almost 11 years
    Shows the size of the directory record rather than full directory size.
  • nikoskip
    nikoskip over 9 years
    @hawkeye for 1st level you can use --max-depth=1
  • Lekensteyn
    Lekensteyn over 9 years
    @nikoskip nice tip! Also nice to know is the shorthand, --max-depth=1 can be shortened to -d1.
  • muru
    muru almost 9 years
    This doesn't show the size of the directory.
  • Whimsical
    Whimsical about 8 years
    given answer didnt give me details for simlinks(ls -lhR), the numbers listed next to it didnt correspond to the right size of each of the values either. My answer gave me a hierarchy with a defined tree structure and the correct answer....OR are you referring to some other upvoted answer?
  • Fabby
    Fabby about 8 years
    Down-vote retracted, upvoted. Please edit and put the comment in the answer... Thanks for the explanation. ;-)
  • SomethingSomething
    SomethingSomething almost 7 years
    Maybe the most amazing software recommendation I have ever got
  • kRazzy R
    kRazzy R over 6 years
    how to get all of these in a decending order of size..
  • kRazzy R
    kRazzy R over 6 years
    how do I view the contents and empty : /dev/sda5 ext4 430G 381G 27G 94% / my laptop is less than 1 month old and it is so full which it makes no sense
  • dylanh724
    dylanh724 over 6 years
    What to use for max depth? -1?
  • dvdmn
    dvdmn about 5 years
    this should be the approved answer
  • Eric
    Eric almost 5 years
    -h show size in human readable way, e.g tree -s
  • qwr
    qwr over 4 years
    4K is not the sizes of the directories
  • Jacquot
    Jacquot over 4 years
    ncdu is really so underrated