Solved

Linux find command: usernames

Posted on 2006-11-21
10
713 Views
Last Modified: 2010-07-27
I am having trouble with the "find" command. I want to find all files that start with DBF in the /tmp directory that are older than 6 hours and are owned by users starting with a user name of user1.

So for example:

If user123 has file DBF1 in the directory and it is older than 6 hours, delete it
If user112 has file DBF2 in the directory and it is older than 6 hours, delete it.
etc.

So here is what I have:

find /tmp -name "DBF*" -mmin +360 -user "user1*"  -ok rm {} \;

I get this error when I run the above command:

find: invalid argument `user1*' to `-user'

How can I use a wildcard for the username?
0
Comment
Question by:bfilipek
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 10

Expert Comment

by:ssvl
ID: 17988210
use user id insted of user name
0
 

Author Comment

by:bfilipek
ID: 17988226
Wont work. I need my search to work based on username.

It works if I do

find /tmp -name "DBF*" -mmin +360 -user user123  -ok rm {} \;

but that only runs for 1 out of my 200 users. I need it to work with all users that start with "user1".
0
 
LVL 10

Expert Comment

by:ssvl
ID: 17988257
The error baz of no user in this name user1 create a user .
0
 

Author Comment

by:bfilipek
ID: 17988260
I also tried:

find /tmp -name "DBF*" -mmin +360 -user "user1[01-200]"  -ok rm {} \;

same error.
0
 
LVL 10

Expert Comment

by:ssvl
ID: 17988287
if the username  does not appear as a login name in the /etc/passwd file, it is taken as a user ID

i wont think it supports a wild card in this option try to write a script
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:bfilipek
ID: 17988349
I am using the user ID not the username, sorry for the confusion. If I check /etc/passwd, this is what I see for the users:

user101:x:675:100:forklift 101:/home/user101:/bin/bash
...
user123:x:697:100:forklift 123:/home/user123:/bin/bash
user124:x:698:100:forklift 124:/home/user124:/bin/bash
user125:x:699:100:forklift 125:/home/user125:/bin/bash
etc.
0
 
LVL 1

Accepted Solution

by:
dnb earned 500 total points
ID: 17988719
The '-user' option only accepts a single user id or user name so you can't do it that way.  (Well, unless you want to use a bunch of ORs!).

find /tmp -name 'DBF*' -mmin +360 -printf '%u:%f\n' | grep '^user1' | cut -d: -f2- | xargs rm
0
 
LVL 6

Expert Comment

by:bpeterse
ID: 17988838
How about putting the following in a script and cron the script:  

for i in `grep user1 /etc/passwd|nawk 'BEGIN { FS=":" }; { print $1 }'`
do
           find /tmp -name "DBF*" -mmin +360 -user $i  -ok rm {} \;  
done      
         

I usually do this in korn or bash.

0
 

Author Comment

by:bfilipek
ID: 17989028
dnb your script worked great, than you.
bpeterese I havent tried yours yet but will just to have another method of doing this.

Do either of you know of a way that I can check to see if DBF* files exist by the same user, and delete all but the most recent one? So if user123 has three files: DBF1A modified at 12:55, DBF1B modified at 12:57, and DBF1C modofied at 12:59, delete all except the DBF1C file because it is newest?
0
 
LVL 1

Expert Comment

by:dnb
ID: 17994211
find /tmp -name 'DBF*' -mmin +360 -printf '%u:%T@:%f\n' | grep '^user1' | sort -t: -r -n | awk 'BEGIN { FS=":" } { if ($1 == currUser) { printf("%s\n", $3) } else { currUser=$1 } }' | xargs rm

The %T@ in the output format of the find adds the modification time of the file (numeric), the results are sorted by user name and modification time in reverse order (descending), the awk spits out the filename of the first entry for each user and finally xargs passes batches of the filenames to rm to be removed.

You may want to try with "xargs echo" on a first run to make sure everything is as it should be.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now