share
Unix & LinuxView size and owner on folders, within a folder
[+1] [2] Hultman
[2020-04-15 12:07:10]
[ linux disk-usage ownership ]
[ https://unix.stackexchange.com/questions/580197/view-size-and-owner-on-folders-within-a-folder ]

Anyone know how to view all the folders within an directory with size, folder/file, owner?

The only command I know of is du -hs * But that shows all the subfolders aswell and does not show owner.

For example, I would like to get the info size, folder/file, owner of the folder/file under "/my/path/".

Any know of command which could provide me with this info?

Br Hultman

[+1] [2020-04-15 12:59:17] AdminBee [ACCEPTED]

You could try the find command:

find /my/path -maxdepth 1 -type d -printf "%u %g   " -exec du -h --max-depth=0 {} \;

should locate all directories (filter -type d) one level below the starting point /my/path (option -maxdepth 1). It will then

  • use the -printf option to print owner and group, and then
  • invoke du --max-depth=0 on each directory found ({}) to print the name and total size directly behind the output of the preceding -printf option, using the -exec mechanism.

Thanks that was exactly what I was looking for! Appreciate it! - Hultman
1
[0] [2020-04-15 12:23:05] Valentin Bajrami

Here I've got a simple snippet for you

#!/usr/bin/env bash

unset fname owner size i

for f in "$@"
  do
    fname[i]="$f"
    owner[i]=$(stat -c %U "$f")
    size[i++]=$(stat -c %s "$f")
  done 

for i in "${!fname[@]}"
  do
    printf "User %s owns %s and the size is %d Kbytes\n" "${owner[i]}" "${fname[i]}" $((${size[i]} / 1024))
  done

Make the script executable or run it like bash script_name [path]

chmod u+x script_name

And run it

./script_name /my/path/*

2