List number and size of files per directory in linux
From time to time you might want to know which directories are particularly large or contain a vast number of files. For that purpose one can use graphical tools such as Disk Usage Analyzer (Gnome) or Filelight (KDE). In case, we mainly work on the command line we may want to get along without graphical tools.
List file size
For the detection of large data sets following command can be used:
du -hd1 | sort -h
As output, you get a list of subdirectories in the current directory and the amount of data in the respective directories, including all subdirectories. The list is sorted by size.
In detail: the command du
(disc usage) determines the disc usage of all first level sub directories (-d1
) and returns the size in Byte, Kilobyte,… (-h
). Piping the output to sort -h
sorts the results according to their size.
List number of files
A list, similar to the file sizes, can be obtained to check how many files are stored in a directory with the following command:
find . -xdev -type f | cut -d "/" -f 1,2 | sort | uniq -c | sort -n
The output lists the number of files contained in a directory, including subdirectories, sorted by number of files.
In detail: the command find
finds all files (-type f
) within the current directory and on the same drive (-xdev
) and returns a list of all files with full path. Piping the list to cut -d "/" -f 1,2
strips everything but the first directory from that list, by cutting every entry at “/” and just keep the first and second fields (-f 1,2)
. Piping to sort and to uniq finds unique entries and counts them (-c
).
The resulting list is piped again to sort by number (-n
).