LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Script help - delete files older than 45 days but exclude the system files (https://www.linuxquestions.org/questions/linux-software-2/script-help-delete-files-older-than-45-days-but-exclude-the-system-files-648677/)

jojothedogboy 06-11-2008 08:38 PM

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.

unSpawn 06-11-2008 09:09 PM

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.

jojothedogboy 06-13-2008 02:48 PM

Quote:

Originally Posted by unSpawn (Post 3182056)
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!

unSpawn 06-13-2008 03:43 PM

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


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