- Code: Select all
du -sh ~/.Trash/ | awk '{print "Trash: " $1}'; ls -l ~/.trash | wc -l |awk '{(contents = $1-1); if (contents <0) print "Trash empty"; else print contents " item(s)"}'
The Unix word count command wc -l counts the number of items in the .Trash directory, and the -l switch makes it count lines instead of words. Because the ls command lists the directory size as well as the contents it is necessary to subtract 1 from the variable "contents" to get the correct number of items. This gives a value of -1 to "contents" when the trash is empty, hence the expression if (contents <0).
Obviously, this script can be adapted to count items in other directories than Trash, so please post anything you derive from this.
Also, is it possible to put the Trash size and number of items on the same line?