Solved

Linux find command: usernames

Posted on 2006-11-21
10
714 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

920 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

14 Experts available now in Live!

Get 1:1 Help Now