LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-14-2010, 05:37 PM   #1
Exoskeletor
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Rep: Reputation: 0
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

Last edited by Exoskeletor; 01-14-2010 at 05:52 PM.
 
Old 01-14-2010, 05:56 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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 {} \;

Last edited by neonsignal; 01-14-2010 at 06:01 PM.
 
Old 01-14-2010, 05:59 PM   #3
Exoskeletor
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
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?

Last edited by Exoskeletor; 01-14-2010 at 06:45 PM.
 
Old 01-14-2010, 07:06 PM   #4
Exoskeletor
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
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?

Last edited by Exoskeletor; 01-14-2010 at 07:09 PM.
 
Old 01-14-2010, 07:30 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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
 
Old 01-14-2010, 07:34 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,247

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
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.
 
Old 01-14-2010, 07:43 PM   #7
Exoskeletor
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
the use of {} is the same with '{}'?
 
Old 01-14-2010, 08:39 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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

Last edited by jschiwal; 01-15-2010 at 01:21 AM.
 
  


Reply

Tags
delete, files, script, shell, username


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Script to own files owned by deleted users KnightHawk Linux - Server 2 03-20-2009 05:48 PM
Need Script to own files owned by deleted users KnightHawk Linux - Server 1 03-20-2009 04:11 PM
is there a way to list all files owned by a user? KingPanda Debian 3 10-29-2008 08:16 PM
Shell script deletes folder before due OR13 Linux - General 3 08-04-2006 08:10 PM
help with a script that deletes files more than X days old BrianK Linux - General 5 06-14-2004 09:05 PM


All times are GMT -5. The time now is 06:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration