LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-11-2008, 08:38 PM   #1
jojothedogboy
LQ Newbie
 
Registered: Jun 2008
Posts: 12

Rep: Reputation: 0
Script help - delete files older than 45 days but exclude the system files


Not sure where to ask this so here it goes:
I admin an FTP server that has multiple users all rooted under the /data dir. I am creating a script to find all files in /data that are older than 45 days and deleting them. I have this script:

Code:
#! /bin/bash
TODAY=$(date +"%Y%m%d")
# todays date
TIME=$(date +"%R")
# the time now
DIR="/home/USERNAME/45day_delete"
LOGFILE="$DIR/45day_delete-$TODAY.log"
#Where to put the log file
echo "start" - $TIME >> $LOGFILE
find /data -mindepth 3 -mtime +45 -type f -print0 -exec rm {} \; | xargs -0 ls -la >> $LOGFILE
# start looking three levels deeper than /data, ex /data/group/user/
echo "end" - $TIME >> $LOGFILE
currently I have it running minus the "-exec rm {} \;" part so it logs all of the files it would delete. I notice that part of the result set are the .bash* files in the top level of each user's dir. I need to exclude the .bash* files and make sure that this script will react as expected.

I also have a similar file to delete all the ._* files created by Mac systems:

Code:
#! /bin/bash
NOW=$(date +"%Y%m%d")
TIME=$(date +"%R")
DIR="/home/USERNAME/DotUnderscore"
LOGFILE="$DIR/DotUnderscore-$NOW.log"
echo "start" - $TIME >> $LOGFILE
find /data -mindepth 3 -name ._* -type f -print0 -exec cp {} $DIR/copiedfiles/ \; | xargs -0 ls -la >> $LOGFILE
#find /data -mindepth 3 -name ._* -type f -print0 -exec rm {} \; | xargs -0 ls -la >> $LOGFILE
echo "end" - $TIME >> $LOGFILE
I have the script copying the files for now (as a test) and all seems to work fine, nothing but ._* files but... I have seen that there are multiple ways of evoking the find command to delete it's results.
Can someone please explain what the differences are between:
Code:
find / -name * | xargs rm
find / -name * -exec rm {} \;
find / -name * -delete
Please help.
 
Old 06-11-2008, 09:09 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Scripting questions often end up in /Programming. Anyway. You could string together exclusions like "and not iname something and not iname something". Wrt "-exec vs xargs", xargs is faster. See for instance http://tldp.org/LDP/Bash-Beginners-G...ect_09_07.html. Not related to 'find' here, but xargs can also be used to work around "too many args" globbing errors.
 
Old 06-13-2008, 02:48 PM   #3
jojothedogboy
LQ Newbie
 
Registered: Jun 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
You could string together exclusions like "and not iname something and not iname something".
How do I do this? What would the command look like? Do I keep it in the one "find" command or is it a separate line in the script? I have read up in the man on "find" and found an "Action" -prune that returns a true/false . I am not sure how this would go together to help me in my script!
 
Old 06-13-2008, 03:43 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Try "find /data -mindepth 3 -mtime +45 -type f -not -iname .bash\* -a -not -iname ._\*"?
 
  


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
bash: delete files older than 7 days in a folder and subfoldes lupomanaro Programming 1 05-07-2008 10:22 AM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 08:31 PM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 08:30 AM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 02:40 PM
delete files in server that is older than 30 days using cronjob latheesan *BSD 2 06-14-2005 12:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:15 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