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

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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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 2

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 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:

[email protected]:~$ 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

Solution 6

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.

Solution 7

This seems to do the trick when simlinks are involved.

ls -LRlh

Solution 8

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.

Solution 9

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

Solution 10

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)

Solution 11

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

du -hd1

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply