0

I want to share a summary of a directory with file names and sizes in plain text.

This thread shows a file list with human-readable file sizes, e.g. on macOS:

$ du -h -d 10
 14G    ./190803 Aaah
 13G    ./190804 (no sound)

This post lists a file structure with a nice tree:

$ find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
.
|____.DS_Store
|____190803 Aaah
| |____190803 Aaah.mp4
| |____DSC_0011.MOV
| |____DSC_0012.MOV
| |____DSC_0013.MOV
| |____DSC_0014.MOV
| |____DSC_0015.MOV
| |____DSC_0016.MOV
|____190804 (no sound)
| |_____DSC0018.MOV
| |_____DSC0019.MOV
| |_____DSC0020.MOV
| |_____DSC0021.MOV
| |_____DSC0022.MOV
| |_____DSC0023.MOV
| |____DSC_0017.MOV

How can I combine both and show the human-readable file size next to each item, file or folder, in the latter tree display?

miguelmorin
  • 5,025
  • 4
  • 29
  • 64

3 Answers3

1

Try this:

find . -print0 | 
  xargs -0 -I% du -h "%" | 
  awk ' { 
      size = $1 
      $1 = "" 
      print $0, "(" size ")" }
  ' | 
  sed -e 's;[^/]*/;|____;g;s;____|; |;g'

If you want the file sizes up in the front of the line, you can try this instead:

find . -print0 | xargs -0 -I% du -h "%" | awk ' { size = $1 ; $1 = ""; print $0, size }' | sed -e 's;[^/]*/;|____;g;s;____|; |;g' | awk ' {size=$NF ; $NF=""; printf("(%5s) %s\n", size, $0) }'

The print0 and -0 deals with cases filepaths with quotes, as in Getting error "xargs unterminated quote" when tried to print the number of lines in terminal .

miguelmorin
  • 5,025
  • 4
  • 29
  • 64
Mark
  • 4,249
  • 1
  • 18
  • 27
  • @miguelmorin - it looks like xargs doesn't like spaces and parens in your file names. I should have known better. I've edited the post to make it more readable and I added some quotes around the arguments passed to du -h by xargs. – Mark Feb 18 '20 at 14:57
  • @miguelmorin - let's try to use this solution https://stackoverflow.com/questions/11649872/getting-error-xargs-unterminated-quote-when-tried-to-print-the-number-of-lines - I will update both commands above to deal with quotes – Mark Feb 19 '20 at 20:27
  • @miguelmorin - in testing, I created a file in a directory like this: `> can't`. This broke the original script - I got the `unterminated quote` message. However, adding the -print0 and -0 removed the error. You should be good to go. – Mark Feb 19 '20 at 20:33
0

You can try this:

for f in $(ls -R); do wc -c $f;done
  • I get this error on the first directory: `wc: 190803: open: No such file or directory` and `wc: Aaah: open: No such file or directory`. Running only `wc -c` on that directory gives this error: `wc: 190803 Aaah/: read: Is a directory`. – miguelmorin Feb 18 '20 at 12:16
0

An app which will do this fast and painlessly is GrandPerspective - it makes graphic maps of the folder sizes but can also save a text list. Free from SourceForge or low cost from the App store.

Jeremy Young
  • 184
  • 1
  • 7