UNIX script for cleanup


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX script for cleanup
# 1  
Old 08-27-2015
Question 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.
# 2  
Old 08-27-2015
What operating system and shell are you using?

What have you tried to solve this problem?
# 3  
Old 08-27-2015
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.

Last edited by rbatte1; 08-27-2015 at 11:57 AM.. Reason: Added ICODE tags
# 4  
Old 08-27-2015
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.
# 5  
Old 08-30-2015
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.
# 6  
Old 08-31-2015
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:
Code:
[ -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???
# 7  
Old 09-03-2015
Here is a careful cleanup script that works without your notification requirement, and without your appkeep requirement that you added in post#5.
Code:
 cat cleantmp.sh

Code:
#!/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
Code:
 /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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Suggestion with script to cleanup

I need help with sed and awk scripts to search for Symmetrix ID=000090009902 and then grep its child disk devices associated to the dead paths and display them only, so that those dead devices can be removed. test01:/#powermt display dev=all Pseudo name=hdiskpower0 Symmetrix ID=000090009902... (0 Replies)
Discussion started by: aix_admin_007
0 Replies

2. Shell Programming and Scripting

pid.cleanup script.

Hi guys! I have a directory in the production environment from which i have to delete files older then 40 minutes with .pid extention. I wrote a script below for the purpose. #!/bin/bash # # Script to delete specific file older than N minutes. # OLDERTHAN="40" #40 minutes ... (6 Replies)
Discussion started by: sajid.shah
6 Replies

3. UNIX for Advanced & Expert Users

Table Cleanup Script

I needed some help with a script to fetch and delete all records prior to 3 days from now connecting to sybase from sunos. I wrote the following script but not working..can someone please guide me with my code. Thanks #!/bin/ksh ##GET PREVIOUS DAY DATE dt=`date | awk... (3 Replies)
Discussion started by: moe458
3 Replies

4. Shell Programming and Scripting

Cleanup Weird character in Unix

Hi, I have a pipe delimited file and I am running into an issue where a field is having some weird character and I am not able to clean it up. It is 2nd field and the weird character is M- . See below. cat -v test.dat 02169| M- PATRICKM- MCALEER |Y 01318| M- LARRYM- PETERSON |Y 30319|... (5 Replies)
Discussion started by: msalam65
5 Replies

5. Shell Programming and Scripting

Mail cleanup from ksh script, keeping 50 most recent msgs

I found some posts describing how to completely clean out a mailbox in Unix/Linux. But I want to keep the 50 most recent messages. Any ideas out there? Thanks! (3 Replies)
Discussion started by: OPTIMUS_prime
3 Replies

6. Shell Programming and Scripting

Suggestions/cleanup Bash script

Hello, beginner bash scripter here.. I was able to write a script and it works just fine. I'm just wondering if someone could chime in or any suggestions to make it cleaner or tighter so to speak. I have a disk to disk backup solution which uses 250GB disks. When one gets full I just po in a new... (7 Replies)
Discussion started by: woodson2
7 Replies

7. UNIX for Dummies Questions & Answers

What is the default temp dir for UNIX AIX 5.3 and cleanup guide.

Hi, I'm new with UNIX. In fact I'm just an Oracle Apps programmer runs and maintaining the system in UNIX platform. I've got this question, what is the UNIX AIX 5.3 default temp directory. Is it /tmp or /var/tmp. Additional to that I was noted that OS also created temp/swap files in this... (1 Reply)
Discussion started by: Alikuching
1 Replies

8. Shell Programming and Scripting

Cleanup script

Hi! I would like to write a script which remove some files, all beginning with the same prefix : prefix.1 doc/prefix.2 ../prefix.3 etc. So, I would create a file and chmod it executable. But I dont know how to pass a variable to a script. I would like to write something like ... (2 Replies)
Discussion started by: tipi
2 Replies

9. Shell Programming and Scripting

User Cleanup Script

Hi Guys, I've got an system setup to act as an sftp server. I have a script that allows me to create chroot users running a custom shell within their home directory, it also creates a subdirectory that they can write into. I'm trying to write a script (that I can cron at a later date) that checks... (3 Replies)
Discussion started by: King_Brucie
3 Replies

10. Shell Programming and Scripting

awk/sed/ksh script to cleanup /etc/group file

Many of my servers' /etc/group file have many userid's that does not exist in /etc/passwd file and they need to be deleted. This happened due to manual manipulation of /etc/passwd files. I need to do this for 40 servers. Can anyone help me in achieving this? Even reducing a step or two will be... (6 Replies)
Discussion started by: pdtak
6 Replies
Login or Register to Ask a Question