Link to home
Start Free TrialLog in
Avatar of bfilipek
bfilipek

asked on

Linux find command script

I need a script that runs on the /tmp directory that counts the number of files that each user owns, and email that resulting number using mutt.

So, lets say I have 3 users:

user101
user123
user199

User101 has 4 files in the /tmp directory, user123 has 1 file in the /tmp directory, and user199 has 10 files in the /tmp directory.

I want an email created that says:
user101 has 4 files
user123 has 1 file
user199 has 10 files

How can I accomplish this with a script?
Avatar of ozo
ozo
Flag of United States of America image

find /tmp -user user101 -print | wc -l
Avatar of bfilipek
bfilipek

ASKER

How can I send the result to an email? Also, that only works for one user, I have 200.
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
for user in list_users ; do
  echo $user has `find /tmp -user $user -print | wc -l` files
done | mail
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, in case you are looking for a list of user starting with some string like one of your previous question, just add a pipe and grep

ls -l /tmp | grep ' user1' | awk 'NR != 1 {Count[$3]++} END {for (i in Count) printf("%s has %d files.\n", i, Count[i]);}'
Thanks to amit_q and Tintin. This is what I ended up using:

ls -l /tmp | awk 'NR != 1 {Count[$3]++} END {for (i in Count) printf("%s has %d files.\n", i, Count[i]);}' | mail -s "Some subject" emailaddr

Works great!