Print file count owned by a given user


Print file count owned by a given user



How to print the number of files owned by every user in a given directory?
So far I am printing user name and disk size. However I want to report out the output of "find $dirname -user <user_name>| wc -l" and print that as a third field in the below printf


find $dirname -user <user_name>| wc -l


set dirname = "a/b/c"

find $dirname -printf "%u %sn"




2 Answers
2



I have a feeling you want to do something like this :


find $dirname -printf "%u %sn"
| awk '{s[$1]+=$2;c[$1]++}END{for(u in s) print u,s[u],c[u]}



This will print a list of users with total accumulated file-size and the total amount of files.





Exactly what I was trying to get at! Did a lot of circus and foreach loops with find <dir_name> -user <user_name | wc -l followed by saving the contents into an array. Even then was lost on how to type the array as a 3rd column! Thank you :)
– Rancho
Jun 29 at 15:19



You can use stat id and awk to do something along these lines:


stat


id


awk


$ cd /var
$ stat -f '%u%t%N' * | awk -F$"t" '{fn[$1][$2]}
END{for (u in fn){
system("id -nu " u)
for (e in fn[u]) print "t" e
}}'



Prints


_jabber
jabberd
root
log
rpc
netboot
lib
db
agentx
run
mail
folders
vm
backups
spool
tmp
install
audit
root
empty
yp
msgs
_mobileasset
ma
daemon
rwho
at
_networkd
networkd



As written, it does not support file names with t in them. Not a terribly hard fix (just concatenate the fields greater than $2 joined by t), but you get the idea.


t


t





Should this not be stat -c (man stat states: _ The valid format sequences for files (without --file-system):_, i.e. the format specifiers don't work with -f)
– kvantour
Jun 29 at 8:56


stat -c


man stat


-f





Since stat is not POSIX, it may vary by system. On BSD / OS X, there is no -c command line switch for stat. Nor is there a -printf option to find so this is an area varies. On Linux, find with printf properly formatted is likely the best bet or stat with the translated switches.
– dawg
Jun 29 at 14:52



stat


-c


stat


-printf


find


find


stat





Thanks that's an interesting way to do it! I was able to get it work
– Rancho
Jun 29 at 15:18






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV