Delete Directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete Directories
# 1  
Old 04-18-2012
Delete Directories

Hello All,

My shell script runs everyday to update certain database. Everytime the script runs it creates a directory, with "current date". These directories contain log and data files:

Code:
 
HOME_DIR/database_name/20120417/*
HOME_DIR/database_name/20120416/*
HOME_DIR/database_name/20120415/*

Now, when I run the update on "20120418", I want to delete "20120415" --- therefore keeping only 3 updates at a time. When it updates on 20120419 - i should only have 20120416 & 20120417 directories.

Is there a way to incorporate this in my script ???

Thanks in advance !

Last edited by ad23; 04-18-2012 at 06:24 PM.. Reason: Typo error -> I want to delete "20120415" and not "20120416". Therefore keeping the last 3 latest directories.
# 2  
Old 04-18-2012
Hi,

try this:

Code:
rm -rf /backupdir/"$(date -d "-2 day" +"%Y%m%d")"*

# 3  
Old 04-18-2012
Thanks! I made a typo in my previous message:

"I want to delete "20120415" and not "20120416". Therefore keeping the last 3 latest directories"
# 4  
Old 04-18-2012
Please post what Operating System and version you are running and what Shell you use.

Does this GNU date command work on your system?
Code:
date -d "-2 day" +"%Y%m%d"


At exact time of day will this deletion process run? (This question is important).

Last edited by methyl; 04-18-2012 at 08:28 PM..
# 5  
Old 04-18-2012
opensuse 11.4 on i686, bash 4.1.10
You can use this command in cron.
Code:
crontab -e

and put a line like
Code:
30 23 * * * /root/myscriptDelete.sh

for execute the command at 11:30 pm
# 6  
Old 04-19-2012
I am using the following version

Code:
 
SUSE Linux Enterprise Server 10 (x86_64)
VERSION = 10

Yes, the command works on my system.

Code:
date -d "-2 day" +"%Y%m%d"

Also, this deletion will occur everyday at around 6pm...
# 7  
Old 04-23-2012
Any suggestions?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete duplicate directories?

Is there a way via some bash script or just cmd to find duplicate directories? i have main folders: TEST1 TEST2 In folder TEST1 is some amount of same folders as in folder TEST2 can be this done? i tried fdupe but it only search for dupe files not whle dirs thx! (8 Replies)
Discussion started by: ZerO13
8 Replies

2. Shell Programming and Scripting

How to delete directories and files with xargs?

Hello, i have an dynamical apache_cache that I need to clean all days (older tant one day) with an unix command : find /usr/IBM/HTTPServer/apache_cache/ -name '*' -mtime +1 -print0|xargs -0 rm -r -- but it didn't work. Could you explain me why. So I will put below all my script :... (13 Replies)
Discussion started by: steiner
13 Replies

3. Red Hat

How to delete only directories alone?

Hi, I need to delete (remove) only directories. How to achieve this? Plz help (4 Replies)
Discussion started by: karthick nath
4 Replies

4. Shell Programming and Scripting

Delete File from Directories

Hi All, I am trying to delete Files from a directory using the below command find $/test/abc/xyz -mtime +30 -exec rm -f {} \; The above command is deleting the files more than 30 days. The problem is there are some subdirectories in xyz, when i execute the above command its trying to delete... (3 Replies)
Discussion started by: ch33ry
3 Replies

5. Shell Programming and Scripting

Script to delete old directories

Hi, I have a requirement like, I need to create the directory with date and time stap (i.e YYYYMMDDHMS) every day end need to delete the old directories which is 12 months old. I have tested with following script cd /export/home/sbeeravo/; find . -type d -mtime +365 -exec rm -rf {} \; ... (2 Replies)
Discussion started by: ShirishaReddy
2 Replies

6. Shell Programming and Scripting

Script to delete old directories

Hi I have 10 directories.I want to delete all the dicrectories except the three recent directories based on timestamp.I am new to BASH can u help me? (1 Reply)
Discussion started by: newuser_25
1 Replies

7. Shell Programming and Scripting

delete and check directories

hi im so sorry i do not have time im doing an urgent cleanup. I have to check on directories for example: /export/home/itbr /export/home/bscsmig there are more but i have the list. how do i write a ksh script in for loop to check those directories and delete display a message if it was... (1 Reply)
Discussion started by: sbavanis
1 Replies

8. UNIX for Dummies Questions & Answers

Delete Directories and Contents

How to delete all subdirectories and contents? oh, and if they are hard linked? (1 Reply)
Discussion started by: t4st33@mac.com
1 Replies

9. UNIX for Dummies Questions & Answers

Delete old home directories

I have a script that deletes obselete users from /etc/passwd then moves their home directories to another location. After 30 days, I need to delete the home directories that were moved to the new location. I would appreciate any ideas on how to delete the directories after the 30 days? (2 Replies)
Discussion started by: munch
2 Replies

10. Shell Programming and Scripting

need some help with a script to delete directories.

Hello, I need a hand with a script im trying to make. I have a directory /usr/db/mail with a bunch of subdirectories such as /usr/db/mail/domain1.com /usr/db/mail/domain2.com etc. etc. all of these directories in turn have sub directories of their own. What I need to do is search all of... (1 Reply)
Discussion started by: centrino
1 Replies
Login or Register to Ask a Question