Solved

Linux find command: usernames

Posted on 2006-11-21
10
717 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

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…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

839 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