LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 08-09-2007, 06:31 PM   #1
ocavid
LQ Newbie
 
Registered: Mar 2005
Posts: 24

Rep: Reputation: 15
Question Script that will auto erase specified files in users directory


hi there,

has anyone has a script that will automatically erase files saved in the users directory?

for example,

We have a samba domain and file server and all users files are in this server, they are only allowed to save document related files, no executable, mp3 and other unnecessary files.

any help or guide is appreciated.

thank you,
ocavid
 
Old 08-09-2007, 07:21 PM   #2
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
You mean like you want a script you can run from cron which deletes any files that aren't (for example) .doc and .xls (whitelist)? Or do you need it to delete files of certain kind (blacklist)?
 
Old 08-09-2007, 07:49 PM   #3
ocavid
LQ Newbie
 
Registered: Mar 2005
Posts: 24

Original Poster
Rep: Reputation: 15
Thumbs up

Yes, script that can run on cron is fine, that can just leave files allowed.

blacklisting is very good i guess but that might be very difficult.

thank you,
ocavid
 
Old 08-09-2007, 08:19 PM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Code:
find /users \
  ! -iregex '.*odt' \
  ! -iregex '.*txt' \
  ... \
  -exec rm {} \;
(or '| xargs rm')

might work. Just reverse the logic and the extensions for removing disallowed files rather than sparing allowed files.

However, you might want to work something up with 'file' since your users can just change the extensions of the files if they want this way.

Not sure if there are effects I'm not thinking of here, so these suggestions comes without warranty.
 
Old 08-09-2007, 10:20 PM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Yeah, here's an approach I came-up with based on the file command:
Code:
#!/bin/sh

if file $1 | egrep "PDF|Microsoft" > /dev/null ; then
  exit ; else
  rm -f $1
fi
Let's say you name it auto-delete.sh and place it in root's home folder. To have the script run every night at 10:30PM you could call this script from root's crontab like:
Code:
30 22 * * * find /home/example -type f -exec /root/auto-delete.sh {} \; > /dev/null 2>&1
Where /home/example is the directory you wanna check (recursively). The script as posted would delete any files that aren't either PDF or Microsoft (I tested it with Excel and Word files). You can easily add matches for other file types. A couple things you might wanna consider are moving the files instead of deleting them (as a safeguard) and having the script generate a log of what it does (and perhaps have it email it to you).

EDIT: Here's an example with logging:
Code:
#!/bin/sh

if file $1 | egrep "PDF|Microsoft" > /dev/null ; then
  echo `date` $1 IGNORED >> /var/log/auto-delete.log ; exit ; else
  echo `date` $1 REMOVED >> /var/log/auto-delete.log ; rm -f $1
fi
I just noticed, however, that my approach has issues when it comes to files with spaces in their names. =(

Last edited by win32sux; 08-09-2007 at 11:53 PM.
 
Old 08-10-2007, 01:07 AM   #6
ocavid
LQ Newbie
 
Registered: Mar 2005
Posts: 24

Original Poster
Rep: Reputation: 15
Thank you!

this is great, this a good start.

Have a great day!
 
  


Reply



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 for cron to erase MT files DavidHB Linux - Software 4 04-30-2007 06:51 AM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 08:30 AM
Auto Delete oldest files @ratex when directory is 98% full jmanjohn61 Linux - Software 1 04-05-2005 03:44 PM
What are core files in the users home directory draven Linux - Newbie 3 03-07-2004 09:56 PM
alter boot floppy to auto erase hdd then shutdown NewtonMan Linux - Software 3 11-14-2003 02:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 02:56 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration