76

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.

4
  • 1
    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' ?
    – Pavlos G.
    Aug 18, 2011 at 12:09
  • Are you looking for software which helps you to find big files, or something to get a (collapsable) file tree?
    – Lekensteyn
    Aug 18, 2011 at 12:09
  • 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.
    – hawkeye
    Aug 18, 2011 at 12:14
  • @J G - Check my update ;-)
    – Pavlos G.
    Aug 18, 2011 at 12:21

11 Answers 11

83
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.

4
  • Just what I was looking for. Thank you very much!
    – Mathlight
    Sep 6, 2016 at 19:21
  • Maybe the most amazing software recommendation I have ever got Sep 14, 2017 at 10:35
  • this should be the approved answer
    – dvdmn
    Apr 29, 2019 at 14:38
  • ncdu is really so underrated
    – Jacquot
    Jan 30, 2020 at 10:31
67

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.

1
  • What to use for max depth? -1?
    – dylanh724
    Dec 16, 2017 at 6:30
36

To get the total size of a directory and all children

du -hs directory/*
2
  • I'd like the next level down from that - the recursive sizes of all the first level children.
    – hawkeye
    Aug 18, 2011 at 12:19
  • 1
    I'll edit my answer. You need to use du -h directory/* Aug 18, 2011 at 12:26
17

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.

4
  • Should sizes be shown?
    – hawkeye
    Aug 18, 2011 at 12:15
  • J G: The -s shows the sizes. Aug 18, 2011 at 12:17
  • 3
    Shows the size of the directory record rather than full directory size.
    – Steven Lu
    Aug 3, 2013 at 22:40
  • -h show size in human readable way, e.g tree -s
    – Eric
    Jul 22, 2019 at 7:09
16

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

0
11

A terminal solution is the du command:

du --all --human-readable --apparent-size

(shorthand: du -ah --apparent-size)

du displays the disk usage for each file and directory. The options explained:

  • --all, -a - show sizes for files as well, not just directories
  • --human-readable, -h - show sizes in a human readable format, e.g. 10K (10 kilobytes), 10 (10 bytes)
  • --apparent-size - show the actual file size, not the sizes as used by the disk.
5
  • I just wanted the first-level children
    – hawkeye
    Aug 18, 2011 at 12:23
  • 2
    @hawkeye for 1st level you can use --max-depth=1
    – nikoskip
    Feb 9, 2015 at 15:36
  • 5
    @nikoskip nice tip! Also nice to know is the shorthand, --max-depth=1 can be shortened to -d1.
    – Lekensteyn
    Feb 10, 2015 at 17:49
  • how to get all of these in a decending order of size..
    – kRazzy R
    Nov 15, 2017 at 19:33
  • 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
    – kRazzy R
    Nov 15, 2017 at 20:32
2

This seems to do the trick when simlinks are involved.

ls -LRlh
2
  • 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?
    – Whimsical
    Apr 20, 2016 at 16:56
  • Down-vote retracted, upvoted. Please edit and put the comment in the answer... Thanks for the explanation. ;-)
    – Fabby
    Apr 20, 2016 at 20:47
1

To get a sorted list put everything in MB and sort :

du -m * | sort -n

Or use tool such as DiskReport to generate a report of full disk tree.

0

Another terminal solution with find and sort (by filesize, column 1)

$ find . -maxdepth 1 ! -path . -printf "%s %p\n" | sort -n -k1                              
178 ./somefile.txt
219 ./somefile2.txt
4096 ./c
4096 ./cs2
4096 ./perl
4096 ./python
4096 ./random
4096 ./sh
2
  • This doesn't show the size of the directory.
    – muru
    Aug 20, 2015 at 6:59
  • 4K is not the sizes of the directories
    – qwr
    Jan 7, 2020 at 20:22
0

I like the following approach:

du -schx .[!.]* * | sort -h

where:

  • s: display only a total for each argument
  • c: produce a grand total
  • h: print sizes in a human-readable format
  • x: skip directories on different file systems
  • .[!.]* *: Summarize disk usage of each file, recursively for directories (including "hidden" ones)
  • | sort -h: Sort based on human-readable numbers (e.g., 2K 1G)
-1

For listing size of folder contents with less typing (-d is short for --max-depth)

du -hd1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.