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.