On Fri, May 31, 2024 at 09:18:03PM +0200, Franco Martelli wrote: > On 31/05/24 at 02:18, Greg Wooledge wrote: > > Confusing and useless. I still don't have a better answer than this: > > > > hobbit:~$ tree --du -Fh /tmp/x | grep /$ > > [7.8M]/tmp/x/ > > └── [4.0K] y/ > > It could be improved adding the "-a" switch to show also the hidden > directories and the "--color" switch to the "grep" command but this sadly > doesn't show the expected result (colorized directories) I don't know why: > > ~$ tree --du -Fah /tmp/x | grep --color /$
You're only coloring the trailing / characters. If you want everything from after the last space to the end of the line, you'd want: tree --du -Fh /usr/local | grep --color '[^[:space:]]*/$' Of course this fails to colorize the entire directory name if there's a space in it. > Me too needed to summarize the directory's size thus I wrote a bash function > to accomplish this and added it to my /etc/profile.d/local.sh file: > > duhs() { IFS=$'\n\r' ; for d in $(/usr/bin/find $1 -maxdepth 1 -type d > |/usr/bin/sort) ; do du -hs $d ; done } This, too, will fail if there are spaces in a directory name. You'd need to quote the "$1" and "$d" at the very least. The for d in $(find) part is also quite fragile; I see you tried to work around that by using IFS, but that's not a full fix. Also, you never declared IFS as a local variable in this function, so you will be altering IFS in any shell that actually runs this function. Consider this one instead: hobbit:~$ duhs() { du -hs "${1:-.}"/*/; } hobbit:~$ duhs /usr/local 81M /usr/local/bin/ 4.0K /usr/local/etc/ 4.0K /usr/local/games/ 136K /usr/local/include/ 45M /usr/local/lib/ 29M /usr/local/man/ 8.0K /usr/local/sbin/ 2.2M /usr/local/share/ 484M /usr/local/src/ 44M /usr/local/tcl8.6/ In bash, a glob that ends with / will match directories only. Globs are always sorted, so you can also drop the call to sort. The only issue with this version is if the number of subdirectories is so large that it triggers the "Argument list too long" error. If you want to avoid that, you can use xargs -0: duhs() { printf '%s\0' "${1:-.}"/*/ | xargs -0 du -sh; } As printf is a bash builtin, it can handle an unlimited number of arguments. So this form should work in all cases.