Solved

Linux find command script

Posted on 2006-11-22
7
333 Views
Last Modified: 2010-04-20
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?
0
Comment
Question by:bfilipek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17999508
find /tmp -user user101 -print | wc -l
0
 

Author Comment

by:bfilipek
ID: 17999532
How can I send the result to an email? Also, that only works for one user, I have 200.
0
 
LVL 58

Accepted Solution

by:
amit_g earned 300 total points
ID: 17999588
ls -l /tmp | awk 'NR != 1 {Count[$3]++} END {for (i in Count) printf("%s has %d files.\n", i, Count[i]);}'
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 84

Expert Comment

by:ozo
ID: 17999597
for user in list_users ; do
  echo $user has `find /tmp -user $user -print | wc -l` files
done | mail
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 18000595
Assuming your users are

user1

to

user200

Then you can do

#!/bin/bash
for i in `seq 1 200`
do
  echo "user$i has `find /tmp -user user$ |wc -l` files"
done | mail -s "Some subject" emailaddr
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18000686
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]);}'
0
 

Author Comment

by:bfilipek
ID: 18019324
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!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question