Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to use du command show a slash after directories?

For example:

du -ab /root/test/php-5.4.8/


1781    /root/test/php-5.4.8/main/internal_functions.c.in
973596  /root/test/php-5.4.8/main
3841    /root/test/php-5.4.8/netware/start.c
577     /root/test/php-5.4.8/netware/sendmail_nw.h
8514    /root/test/php-5.4.8/netware
4957    /root/test/php-5.4.8/README.TESTING2
4561    /root/test/php-5.4.8/.gitignore

However I want directories to include trailing slash, for example:

1781    /root/test/php-5.4.8/main/internal_functions.c.in
973596  /root/test/php-5.4.8/main/
3841    /root/test/php-5.4.8/netware/start.c
577     /root/test/php-5.4.8/netware/sendmail_nw.h
8514    /root/test/php-5.4.8/netware/
4957    /root/test/php-5.4.8/README.TESTING2
4561    /root/test/php-5.4.8/.gitignore

I have managed to do find command but it will not include total file size of the directories and that is why I have to use du command

find /root/test/php-5.4.8/ \( -type d -printf "%s %p/\n" , -type f -printf "%s " -print \)
share|improve this question

Just make it work with a little bash scripting. Print the size and filename, if it is a directory add a trailing slash.

du -ab | while IFS=$'\t' read -r size line; do printf "%s\t%s" $size "$line"; [[ -d $line ]] && printf "/"; echo; done

This will work with any file name not containing newlines or ending with a tab.

share|improve this answer
Thanks! What is [[ -d $line ]] ? – I'll-Be-Back Oct 20 '12 at 20:14
@I'll-Be-Back If exists and is a directory.... – Alex Chamberlain Oct 20 '12 at 20:15
@Gilles Thanks for fix. Can you think of anything to improve the performance more? – I'll-Be-Back Oct 20 '12 at 22:12
@I'll-Be-Back Not without recompiling du. This should be I/O-bound anyway. Maybe changing the loop body to if [[ -d $line ]]; then d=/; else d=; fi; echo -E "$size"$'\t'"$line$d" will slightly speed things up if the output speed is the limiting factor because the file metadata is already in the cache. – Gilles Oct 20 '12 at 22:22
Like this? du -ab | while IFS=$'\t' read -r size line; if [[ -d $line ]]; then d=/; else d=; fi; echo -E "$size"$'\t'"$line$d"; done – I'll-Be-Back Oct 20 '12 at 22:40

My favorite trick with GNU du.

du -chs -- */ *

Since du excludes duplicates on command line arguments, it works, and print the slash automatically since you supplied the slash for folder.

share|improve this answer
That doesn't work as-is for directories that aren't at the toplevel. It does work if you change */ to **/*/ (or **/, depending on your shell) (after turning on the globstar option in ksh or bash), but it may be slower because of the two recursive traversals. Also, the output won't be sorted in a sensible order. – Gilles Oct 21 '12 at 20:20
It can be quite a memory hog, just reached over 70% mem usage on my 8GB machine for du **/'ing my home directory ... – jippie Oct 21 '12 at 20:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.