Script that will Delete and Maintain Files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script that will Delete and Maintain Files
# 1  
Old 12-30-2011
Script that will Delete and Maintain Files

Hi guys,

Good day to all and Happy New Year!!! Cheers!!!

I hope anyone could help/suggest me on hot to create a script that will monitor the directory where files are being dumped. I have a script in CRON that runs everyday and collect for logs with a filename of Output_$(date +%Y%m%d_%H%M%S).log

I hope I have also a script that will delete for the older logs and retain only let's say 3-day logs. For example: (under Directory, i have already 3 files)
Code:
Output_20111228_090500.log
Output_20111229_090500.log
Output_20111230_090500.log

So tomorrow, if the script in collecting logs will run again a file Output_20111231_090500.log will be added to the directory, what should I do in order to have a script that will DELETE the Output_20111228_090500.log and having a new output of:
Code:
Output_20111229_090500.log
 Output_20111230_090500.log
Output_20111231_090500.log

I hope someone will help me, please Smilie


Br,
rymnd_12345
# 2  
Old 12-30-2011
So, when done, you simply want to make sure that only three log files are kept?

As in, on the 30th the directory would have:
20111228-
20111229-
20111230-

And then on the 31st the directory would have:
20111228-
20111229-
20111230-
20111231-

BUT, you want to get rid of the 20111228- file?
# 3  
Old 12-30-2011
Hi joeyg,

Yap, that's it.. That's my point so that whenever the script runs it will delete the old file based on the present date.

Do you have any suggestion/idea?


Br,
rymnd_12345
# 4  
Old 12-30-2011
Well, then when you are done on the 31st, and have:
Code:
Output20111228-
Output20111229-
Output20111230-
Output20111231-

if you do
Code:
>ls Output* | head -1
Output20111228-

And you could delete it with
Code:
rm `ls Output* | head -1`

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Script FTP maintain error handling

Hi, I have ftp script like below How to insert an error handling, If the transfer failed then send mail to me. Actually, I just need the script to send an email if the FTP failed. How to put the email script within FTP script? Thank You Edy (5 Replies)
Discussion started by: edydsuranta
5 Replies

3. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

4. Shell Programming and Scripting

Script to maintain file versions

I am developing a script to maintain 'n' number of versions of a file. The script will take a filename as a parameter and the number of versions to maintain. This basically does something like a FIFO. Here is what I developed. But something is not right. I have attached the script. Can u pls help... (2 Replies)
Discussion started by: vskr72
2 Replies

5. UNIX for Dummies Questions & Answers

script to delete old files

Hi, I want to delete files that are older than 60 days.... i need to execute the script in 7 differnt folders.... i can run the script in crontab to regularly check.... I am struck @ finding out how the file is 60 days old or not... Can u please help me on this? Thanks, NithZ (6 Replies)
Discussion started by: Nithz
6 Replies

6. Shell Programming and Scripting

Script to delete files

Hi, I am looking for a BASH script that deletes old files except the last three recent ones. (8 Replies)
Discussion started by: newuser_25
8 Replies

7. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

8. UNIX for Dummies Questions & Answers

maintain database script...

Hi there. i'm new user at here I need help for this. I need to write a script that maintains a database of cryptographic checksums. In other words, I need this script to check the files checksum whether the files has been modified or not. But i got no idea where to start. Hope anyone here can... (8 Replies)
Discussion started by: hihihehe
8 Replies

9. Shell Programming and Scripting

Maintain full path of a script in a var when sourcing it from a different script

Hi All, I've searched through the forum for a solution to this problem, but I haven't found anything. I have 2 script files that are in different directories. My first script, let's call it "/one/two/a.sh" looks like this: #!/bin/sh IN_DIR=`dirname $0` CUR_DIR=`pwd` cd $IN_DIR... (4 Replies)
Discussion started by: mrbluegreen
4 Replies

10. Shell Programming and Scripting

script to delete files

hi guys, i need a script to delete files that have core in their name ...it might be part of the file name or as a .core extension ...any file that has core as its extension.... i am only able to delete files which just have thier name as core using this : find $1 -type f -name "core"... (12 Replies)
Discussion started by: vats
12 Replies
Login or Register to Ask a Question