directories are one of many types of files (regular, directory, symlink, fifo, device...). Maybe you meant regular files as that's the other type of files beside directories that have disk space allocated for them¹.
du / -ah | head -n 10 | sort -r -n | awk '{print $2}'
Is wrong because with -h
, you get KMGT... suffixes which means sort -n
won't work. The GNU implementation of sort
has a -h
option to decode those suffixes but because of rounding, the order may still be wrong.
Also beware that hard links are counted only once and that file paths may contain newline characters and not be made of text. Not to mention that your awk
returning the second whitespace delimited field will choke on file paths containing whitespace. 0 is the only byte value that can't occur in a file path, so you want to use NUL-delimited records to represent file lists.
-h
is a GNU extension. The GNU implementation of find
can report disk usage with -printf %b
or -printf %k
, so you could do something like:
find . -type f -printf '%b\t%p\0' |
sort -zrn |
numfmt -d $'\t' -z --to=iec --from-unit=512 |
tr '\0' '\n'
Note that the tr
is only to output it for human consumption, if you wanted to post-process that list, you'd want to keep the format NUL-delimited. For instance, to delete the 10 largest (in terms of disk usage) files:
find . -type f -printf '%b\t%p\0' |
sort -zrn |
head -zn10 |
cut -zf 2- |
xargs -r0 rm -f
(-printf
, -z
, -r
, -0
are all GNU extensions, though xargs -0
is now supported by a few other implementations; -r
as well to a lesser extent).
Technically, rm
unlinks the files from their parent directory. You might find that that pipeline returns the same file linked to 10 different directories; you'd then unlink it from them, but if that file is still linked to some more directories, you won't reclaim its space.
If you wanted to delete all the entries (links) in the directories under .
of the 10 largest files, you could do:
find . -type f -printf '%b\t%D:%i\t%p\0' |
sort -zrn |
gawk -F'\t' -v RS='\0' -v ORS='\0' '!seen[$2]++ && ++n > 10 {exit}1' |
cut -zf 3- |
xargs -r0 rm -f
¹ beware that for files of type directory, du
reports the disk usage of the directory file itself, plus that of any unique file that it lists and those of its subdirectories recursively.
find
to filter only regular files towardsdu
.sort
beforehead
, like into the post linked to your previous question, in order to get the files you need.