LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   need shell script that deletes all files owned by a user (http://www.linuxquestions.org/questions/programming-9/need-shell-script-that-deletes-all-files-owned-by-a-user-782270/)

Exoskeletor 01-14-2010 05:37 PM

need shell script that deletes all files owned by a user
 
the script should take as input in the begginig the username of the user and then deletes all the files and folders from the user in every place he has them. script must also check if the parameters have been given correctly (only one and that one must be a username)
any idea guys how i can do something like this?
Doesnt all the files of a user exist on a folder with his name? what if i delete this folder?
Will something like this work?

Quote:

E_NOARGS=65

if [ -z "$1" ] # Exit if no argument given.
then
echo "Usage: `basename $0` directory-to-copy-to"
exit $E_NOARGS
fi
find . -name "*.*"-exec rm -rf {} \;
exit 0

neonsignal 01-14-2010 05:56 PM

If you just want to remove the user, you can 'deluser'
Code:

deluser --remove-all-files username
Removing the user's home directory will remove most files that the user has created.

If you wanted to search the whole system and remove files owned by a specific user, you could do something like this (I'm assuming the user isn't logged in at the time):
Code:

find / -user username -exec rm {} \;

Exoskeletor 01-14-2010 05:59 PM

Quote:

Originally Posted by neonsignal (Post 3826869)
If you just want to remove the user, you can 'deluser'
Code:

deluser --remove-all-files username
Removing the user's home directory will remove most files that the user has created.

If you wanted to search the whole system and remove files owned by a specific user, you could do something like this (I'm assuming the user isn't logged in at the time):
Code:

find / -user username -exec rm {} \;

thanks for the answer. will remove folders also? how i can check if the user has give only one parameter and if a user with this name exist?

Exoskeletor 01-14-2010 07:06 PM

will this
Quote:

E_NOARGS=65

if [ $# -ne 1 ]
then
echo "Lathos dilosi parametron"
exit $E_NOARGS
else
find / -user $1 -exec rm {} \;
fi
do the trick?

jschiwal 01-14-2010 07:30 PM

Add a "-type f" test to the find command.

You could use more than one directory to search for, including all the directories the user has write access to:
e.g.
find /tmp /var/tmp /srv/samba/public/ -user "${username}" -type f -exec rm -f '{}' \;
find /tmp /var/tmp /srv/samba/public/ -user "${username}" -type d -exec rmdir '{}' \;

The second line may fail when there are files owned by other users in a directory owned by the user.
You could include both lines in a block and redirect stderr to a log file.
{
find ...
find ...
} 2>userfiledel.errors

chrism01 01-14-2010 07:34 PM

You need to get the script to check the /etc/passwd file, so ensure you run this before removing the user...
Of course check your man pages for userdel / deluser to see if an all-encompassing option is available.

Exoskeletor 01-14-2010 07:43 PM

the use of {} is the same with '{}'?

jschiwal 01-14-2010 08:39 PM

I've always used '{}'. I think you need it to handle filenames with spaces.

It would be a good idea to check the range of UIDs for regular users so you can't remove a system user's files.

This will extract the values.
Code:

egrep '^(UID|GID)_(MIN|MAX)' /etc/login.defs
Since this won't change for the distro, it would be easiest to assign these variables as constants at the beginning of the script and use the constant variables in the lines which check if the UID is in range.

Given the username, you could use "id" or "getent" to return a users UID. Perhaps you use centralized authentication, e.g. NIS or LDAP, and the user isn't in the /etc/passwd file.

id -ru ${username}

or

getent passwd ${username} | cut -d: -f3


All times are GMT -5. The time now is 11:33 AM.