1832574 Members
5314 Online
110043 Solutions
New Discussion

remove files by owner

 
SOLVED
Go to solution
Derek Baxter_1
Occasional Advisor

remove files by owner

I could not find this here. Can anyone tell me if there is a way to remove files from a directory by the owner of the file. We accidentally restored files to the wrong folder and there are over 1000 files there.
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: remove files by owner

I use a two step process.

ls -1 | grep username > list.txt

Then I read the filelist with the script command

read a

rm $a

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Michael Tully
Honored Contributor

Re: remove files by owner

If it is a certain user, I would be tempted to use the find command to do this.

# find . -user >/tmp/list
# sed 's/^/rm /'
/tmp/list1
# sh /tmp/list1

This wil remove the files from the current directory with a cerain username and not all files.

Regards
Michael
"When I have trouble spelling, it's called fat finger syndrome"
Anyone for a Mutiny ?
Rajeev  Shukla
Honored Contributor
Solution

Re: remove files by owner

or use
find -user -exec rm {}\;

Rajeev
Steve Lewis
Honored Contributor

Re: remove files by owner

A two-step process is good idea, to first check that you are going to remove the right files.

Here is another way:

$find /directoryname -user username -type f | more

Check that it lists only the files you want to remove.

$find /directoryname -user username -type f | xargs -t -i rm {}

This prints the rm command it runs for every file it finds, so you have a record of what you deleted.



Robert-Jan Goossens
Honored Contributor

Re: remove files by owner

Two steps is good. I do this,

# find /dir -type f -user username -exec mv {} /tmpdir \;

# cd /tmpdir

# ls -la

# rm -r *
Trond Haugen
Honored Contributor

Re: remove files by owner

If you are confident that all files in that dir owned by the user sould be removed:
find dir -user username -exec rm {} \;

Regards,
Trond Haugen
Regards,
Trond Haugen
LinkedIn