Monday, July 2, 2007

find / delete files owned by user / group

Here are alternative ways to find files owned by a user or certain group. Incase a person resigned, just incase you would need to backup or delete all files owned by that specific user or group not located from his own folder. Remember to do this using the super duper linux root user, however you need to have extra care as some commands could be irreversible!

If looking for files owned by a user, we need to know the user's UID/GID by looking at password file

# cat /etc/passwd | grep USERNAME

Make sure this spits out a one liner for that specific user, if not, try do restrict the filter or do an exact match.

See my own result:


Jot down the UID field which is 512 and the group field which is 513, you would use it later.

Now, begin search from root location if you want to search from the root, you can change that in any way like /home instead of / .

#find / -uid 512

To look for the files owned by that group

#find / -gid 513

Lastly, combine them with any linux command that you want to do with those resulting files or even an input field for a bash script. Be warned again that this could be irreversible!

Let's say I want to see them first:

#find / -gid 513 -exec ls -la {} \;

Now, let us delete these files owned by that user:

#find / -uid 512 -exec rm -rf {} \;

If you want to delete those files owned by that group id:

#find / -gid 513 -exec rm -rf {} \;

For verification, you can issue your first search commands again if they are really deleted.

That's it.


Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy