[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 741
  • Last Modified:

Linux find command: usernames

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
bfilipek
Asked:
bfilipek
  • 4
  • 3
  • 2
  • +1
1 Solution
 
ssvlCommented:
use user id insted of user name
0
 
bfilipekAuthor Commented:
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
 
ssvlCommented:
The error baz of no user in this name user1 create a user .
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
bfilipekAuthor Commented:
I also tried:

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

same error.
0
 
ssvlCommented:
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
 
bfilipekAuthor Commented:
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
 
dnbCommented:
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
 
bpeterseCommented:
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
 
bfilipekAuthor Commented:
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
 
dnbCommented:
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now