Solved

Linux find command: usernames

Posted on 2006-11-21
10
715 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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
 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Fine Tune your automatic Updates for Ubuntu / Debian
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.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

770 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