Operating System - HP-UX
1834465 Members
2905 Online
110067 Solutions
New Discussion

Re: Interrogate file owner

 
SOLVED
Go to solution
Monte Heeren
Frequent Advisor

Interrogate file owner

Trying to find a script to delete files that are older than AND that are not owned by a certain user.

Syntax used:
find . -depth -mtime +
-exec rm {} \;

This script works great to remove files that are certain days old, but I need to add to it to Not remove the file if it is owned by a paticular user. Any ideas on how to do this?

Monte


12 REPLIES 12
Jeff_Traigle
Honored Contributor
Solution

Re: Interrogate file owner

Add ! -user username

! negates whatever test you put it in front of.
--
Jeff Traigle
A. Clay Stephenson
Acclaimed Contributor

Re: Interrogate file owner

find . -depth -mtime +30 ! -user mickey -exec rm {} \;

will delete all files that have not been modified in the last 30 days and not owned by user "mickey".

When developing any finds like this, it is always better to -exec a safe command like "ls -l" until you are certain that your filters are just right and then switch to the real "rm" command.
If it ain't broke, I can fix that.
Monte Heeren
Frequent Advisor

Re: Interrogate file owner

Thank you Jeff Traigle and A. Stephenson.
That was exactly the answer I was looking for. This forum is priceless!

Monte Heeren
DBA/Systems Engineer
Area Education Agency 11
Johnston IA 50131
Monte Heeren
Frequent Advisor

Re: Interrogate file owner

Can not get the ! operator to work.

If I enter:
find . -user mickey -exec ll {} \;
it will return with only files that are
owned by mickey. This works great.

But if I enter:
find . ! -user mickey -exec ll {} \;
it returns with all the files in the dir,
including the files owned by mickey.

Look like the ! operator is not working.
Is there another "NOT" character to use?

Monte.
TwoProc
Honored Contributor

Re: Interrogate file owner


Are you using "csh"? If so, the ! character means something else. Try putting a backslash "\" in front of it.
We are the people our parents warned us about --Jimmy Buffett
A. Clay Stephenson
Acclaimed Contributor

Re: Interrogate file owner

I suspect that you are ll'ing directories. In that case, eventhough the directory is properly filtered all files under that directory are listed. What you should do is add a '-type f' filter so that only regular files are found.

You didn't bother to indicate your HP-UX version but I would also make sure that the latest find cumulative patch is installed. For 11.11, it's PHCO_30746.
If it ain't broke, I can fix that.
Jeff Schussele
Honored Contributor

Re: Interrogate file owner

Hi Monte,

You need to make it boolean.
Surround the two operators with ( ) - separate them with spaces from the operands AND use -a for the AND operator.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
TwoProc
Honored Contributor

Re: Interrogate file owner

oh, I see, it's returning "." in the set, and this will automatically list everything in any subdirectory which matches the return set. In other words, since "." is returned, everything under "." is listed also.

Try adding "-type f" to the command:

find . ! -user mickey -mtime +30 -type f

We are the people our parents warned us about --Jimmy Buffett
Patrick Wallek
Honored Contributor

Re: Interrogate file owner

To make sure you don't list a directories contents you could also add a '-d' to your 'll':

# find . ! -user mickey -mtime +30 -type f -exec ll -d {} ;

Jeff Schussele
Honored Contributor

Re: Interrogate file owner

Hi Monte,

You need to make it boolean.
Surround the two operators with ( ) - separate them with spaces from the operands AND use -a for the AND operator.
And they need to be escaped with leading \

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Monte Heeren
Frequent Advisor

Re: Interrogate file owner

Thank you all for your quick response.
This is the only place for Unix help!

I added the -type f to the end of the command and it works perfectly.

Monte Heeren.
Monte Heeren
Frequent Advisor

Re: Interrogate file owner

Thank you for your quick response.

I have found the answer and am closing this thread.