3

How can I output all files/directories, ordered by size, including hidden ones
(those whose name starts with a dot), all in one go? 1

The difference to How can I sort du -h output by size is that I'm requesting an output
that includes all files and directories – whether hidden or not.

References


1 By size of a directory, I mean the sum of all file sizes in the directory and all of its subdirectory tree.

Henke
  • 255
  • 3
  • 13

1 Answers1

10

How can I output all files/directories, ordered by size, including hidden ones?

Use the du (disk usage) command, which is part of GNU coreutils : 1

du -hs -- * .[^.]* | sort -h

The .[^.]* regular expression makes sure that hidden files and directories are included.

To list only hidden files and directories :

du -hs -- .[^.]* | sort -h

Some more cases

List only directories, sorted increasing in size :

du -hs -- */ .[^.]*/ | sort -h

List only files, sorted increasing in size : 2

ls -AhlS | grep '^-' | tac

List only hidden files, sorted increasing in size :

ls -hldS .* | grep '^-' | tac

List only regular files, sorted increasing in size :

ls -lS | grep '^-' | tac

References


1 I gratefully attribute my solution to this comment. The -- argument marks the end of options.
The du command can be painfully slow for very large files/folders. Consider using the ncdu command instead.
To install on a Debian derivative, including Ubuntu, run: sudo apt install -y ncdu.
On Arch Linux, including MSYS2, run: yes | pacman -Syu ncdu.
To use it, type ncdu, and press .

2 The -h flag of ls outputs the file sizes in a human-readable style.
The -S flag sorts the output in the order of decreasing size.
The pipe | grep '^-' excludes directories and symbolic links.
The pipe | tac reverses the output.

Henke
  • 255
  • 3
  • 13