UNIX script for cleanup

Hello,

I need some help from unix guru's here..I am looking for some advanced level script to cleanup the directories and files from specific directories under a file system..

The folders are created under /opt/modules
And under modules, there are multiple subfolders with the application names. So, I have to look under /opt/modules and search all subfolders and remove the directories or files keeping only last 10 days and also email me the list of the folders and files getting deleted every day.

Directory:/opt/modules
Files to keep: last 10 days
Also the script should send me an alert when that /opt/modules filesystem gets more than 85%.
And the scripting activity should be logged in /app/tmp/clean-up/clean-up-xxxxxxx.log, with the date and timestamp.

Thanks.

What operating system and shell are you using?

What have you tried to solve this problem?

Hi,

Thanks for the reply. I am looking for a bash shell. Basically, I know how to do this using basic unix commands. But I am looking to see this a more fancy way with some conditions.

For ex, to remove files, find . -name "*.log" -mtime +10 -print and list them and using xargs I can remove them.

And in my script, I have to be able to remove the directories and subfolders which are more than 10 days old on a daily basis.

Thanks.

You need to be VERY clear about your requirements. Do you really want to remove a directory (and the entire file hierarchy rooted in that directory) if that directory hasn't changed in 10 days? That could easily remove lots of files and sub-directories that were created just minutes ago.

Do you really care about the age of a directory? Or, could you just remove every empty directory after removing regular files that haven't been modified in 10 days?

Do you have any files in the file hierarchy you're processing that are not files of types directory or regular file (symbolic links, sockets, FIFOs, block and character special files, ...)?

Are there any space, tab, or newline characters in any of the filenames in the file hierarchy you're processing?

And, I repeat: What operating system are you using? Even on common commands, available options and various utility limits vary widely from system to system. Telling us about your environment helps us provide suggestions that will work for you.

Hello Don,

Thanks for the response, and sorry for little delay.

So here is the scenario..

I am working on Linux. We have couple of folders get updated constantly under /opt/modules. The folders under this are Prd, Cmp. So there are app related folders and files get created under Prd. For ex, under Prd, there are so many folders like App1-2015-08-31-R45, App1-2015-08-30-R44, App1-2015-08-31-R45. So for example, if I have to remove only App1-2015-08-31-R45 and App1-2015-08-31-R42 folders I have to mention that in a config file under /var/temp/appkeep file. Then the process should delete the following folders mentioned in appkeep file and send an email. Also if the folders under /opt/modules/Prd/ are 10 days old, there is another email comes and asking about if we need to delete them or not. Based on it, we will mention that in appkeep file again so that they will delete only those folders.

Please let me know if there are any questions.

Thanks.

OK. Now I'm totally confused.

You have a file named /var/temp/appkeep that contains a list of directories to be deleted (and the age of files in those directories does not matter) when your script is run. That can be done by:

[ -s /var/temp/appkeep ] && rm -rf $(cat /var/temp/appkeep)
> /var/temp/appkeep

And then you have to write another script that will read e-mail sent to you saying that other directories should be added to the current list held in var/temp/appkeep . Writing a script to read a mailbox, understand the subject lines and text of messages in that mailbox, and to reliably determine from the contents of that mailbox what directory names to add to the apkeep file to be sure that those directories will not be kept during the next run of the 1st script above will be a significant research project well beyond the basic programming steps usually discussed in a forum like this.

I don't see how any of this relates to anything you described in post #1 in this thread???

Here is a careful cleanup script that works without your notification requirement, and without your appkeep requirement that you added in post#5.

 cat cleantmp.sh
#!/bin/sh
PATH=/usr/bin:/bin:/usr/sbin
export PATH
max_days=30
owner_to_keep="root nobody"
case $1 in
-ok)
 owner_to_keep=$2
 shift 2
 ;;
esac
case $1 in
[0-9]*)
 max_days=$1
 shift
 ;;
esac
dirs_to_clean=${@:-/tmp}
omit=""
for i in $owner_to_keep
do
 omit="$omit ( ! -user $i )"
done

for dir in $dirs_to_clean
do
 [ -d "$dir" ] &&
 cd "$dir" &&
 find . -depth \! -type d \( -mtime +$max_days -o -mtime -0 \) \( -atime +$max_days -ctime +5 -o -ctime +$max_days -atime +5 -o -type l \) $omit -delete -exec echo RM {} \; -o -type d -empty $omit -mtime +$max_days -delete -exec echo RMDIR {} \; 2>/dev/null
 sleep 1
done
exit 0

Run this as

 /path/to/cleantmp.sh 10 /opt/modules/* > deleted.log

For a first test you can omit the -delete and only look at the deleted.log