Shell script to remove oldest file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to remove oldest file
# 1  
Old 03-19-2012
Shell script to remove oldest file

Hi,
I have two set of files in a directory called /tmp/backup. I want to write a script to remove the oldest of these two, so that there is space available for a newer similar backup file.

For example,
I have
/tmp/backup/dbbackup_dbname_121223112 (oldest)
/tmp/backup/dbbackup_dbname_321445563 (latest)

Now I want my script to remove the oldest
/tmp/backup/dbbackup_dbname_121223112

and if the same script is run next day it should remove
/tmp/backup/dbbackup_dbname_321445563

I am thinking about a command in script such as below

Code:
find rm -f `/tmp/backup/dbbackup_dbname -mtime +2`

(not sure about this, please advice)

Thanks.

Last edited by joeyg; 03-19-2012 at 01:16 PM.. Reason: Please wrap code and data with CodeTags
# 2  
Old 03-19-2012
Something to think about

Code:
ls -1tr | head -1

will provide you with the filename of the oldest file
could you then...
Code:
rm -f 'ls -1tr | head -1'

# 3  
Old 03-19-2012
Try This !!

Code:
find  /tmp/backup/dbbackup_dbname -type f -mtime +3 -exec rm -frv  {} \;

Note: Take care while deciding "+3" value in above will delete all your files 3 days old .


--Shirish Shukla
# 4  
Old 03-19-2012
Joeyg,
Thank you very much for your quick reply.

Your command gives the result as below

ls -1tr | head -1
databasefiles_DB_r8n64lfn_11112_20120317

But what if I have more than one old file as below? Your command would give me the single oldest file, but in my situation I have bunch of old files as shown below. If I run the script today (Mar 19), I want to remove all the files created on Mar 17. Please see below.

Code:
 
                                 120761786368 Mar 17 21:59 databasefiles_DB_r8n64lfn_11112_20120317
-rw-r-----   1 oracle   dba      79084781568 Mar 17 22:13 databasefiles_DB_r9n64mhv_11113_20120317
-rw-r-----   1 oracle   dba      6076250624 Mar 17 22:15 archivelogs_DB_ran64nd9_11114_20120317
-rw-r-----   1 oracle   dba      6033871360 Mar 17 22:16 archivelogs_DB_rbn64nfm_11115_20120317
-rw-r-----   1 oracle   dba      6033692160 Mar 17 22:17 archivelogs_DB_rcn64ni3_11116_20120317
-rw-r-----   1 oracle   dba      5863216128 Mar 17 22:19 archivelogs_DB_rdn64nkg_11117_20120317

-rw-r-----   1 oracle   dba      81461248 Mar 17 22:19 databasefiles_DB_rfn679ro_11119_20120318

-rw-r-----   1 oracle   dba      120761786368 Mar 18 22:40 databasefiles_DB_rhn67bvg_11121_20120318
-rw-r-----   1 oracle   dba      79104376832 Mar 18 22:57 databasefiles_DB_rin67da5_11122_20120318
-rw-r-----   1 oracle   dba      5368697344 Mar 18 22:58 archivelogs_DB_rjn67eaf_11123_20120318
-rw-r-----   1 oracle   dba      5341558784 Mar 18 22:59 archivelogs_DB_rkn67eci_11124_20120318
-rw-r-----   1 oracle   dba      5143271936 Mar 18 23:00 archivelogs_DB_rln67eek_11125_20120318
-rw-r-----   1 oracle   dba      81461248 Mar 18 23:00


Thanks again.

Last edited by methyl; 03-19-2012 at 02:28 PM.. Reason: Code tags & some line-up
# 5  
Old 03-19-2012
Quote:
databasefiles_DB_rfn679ro_11119_20120318
Apart from the fact that this backup appears to start on the 17th and finish on the 18th, there also seems to be two files missing (sequence 11118 and 11120).

It would be logical to treat the start of the Oracle database online backup as a marker point, not just the date. The Oracle database backup and the subsequent archive logs are what you need to recover the database data. You would only delete the old online backup set after a successful offline backup of those files. Does your offline backup start after the online backup finishes?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove Space and blank line from file in UNIX shell script

I have below file. I want to remove space at begining of every line and then after also remove blank line from file. I use below code for each operation. sed -e 's/^*//' < check.txt > check1.txt sed '/^\s*$/d' < check1.txt > check2.txt above code not remove all the space... (12 Replies)
Discussion started by: Mohin Jain
12 Replies

2. Shell Programming and Scripting

Shell script to pick the oldest file

hi i need shell scrip to pick the oldest file... i.e i need a script to pick the oldest file I had input files in my input folder that are created on saturday,monday,tuesday .. i want a script such that first the script should pick saturday file ,then monday and then file created on... (5 Replies)
Discussion started by: hemanthsaikumar
5 Replies

3. Shell Programming and Scripting

Need regex shell script to remove text from file

Hello I am trying to remove a line like <?php /*versio:2.05*/if (!defined('determinator')){ content goes here}?> Now i want to scan all... (6 Replies)
Discussion started by: devp
6 Replies

4. Shell Programming and Scripting

Remove line breaks in csv file using shell script

Hi All, I've a csv file in which the record is getting break into 1 line or more than one line. I want to combine those splits into one line and remove the unwanted character existing in the record i.e. double quote symbol ("). The line gets break only when the record contains double... (4 Replies)
Discussion started by: rajak.net
4 Replies

5. Shell Programming and Scripting

How to remove a file in shell script if its size exceeds limit?

How can i remove a file using shell script when its size exceeds 10MB. Given that file is located in different location to the shell script where it is running? (4 Replies)
Discussion started by: vel4ever
4 Replies

6. Shell Programming and Scripting

Shell script to remove some content in a file

How can I remove all data that contain domain e.g zzgh@something.com, sdd@something.com.my and gg@something.my in one file? so that i only have data without the domain in the file. Here is the file structure "test.out" more test.out 1 zzztop@b.com 1 zzzulll 1 zzzullll@s.com.my ... (4 Replies)
Discussion started by: Mr_47
4 Replies

7. Shell Programming and Scripting

How to remove particular line from file through shell script?

Hi, I am pasring a file line by line. I need to check each field in line and remove particular line. input file lines are, 02;ABC;PQR 03;aaa;rrr 04;ABC;ggg 09;eee;ABC 04;lmn;stu I am looking for line containing "ABC" as field value. Now How can I remove this line from input file... (7 Replies)
Discussion started by: Poonamol
7 Replies

8. Shell Programming and Scripting

Shell Script to remove spaces while writing to the file

Hello Folks, I want to get the results from a SQL query which needs to be exported to a .txt file. My Script is something like #!/bin/ksh db2 connect to DATABASE user user_name using pwd; touch test.txt isResult=0; isResult= `db2 -x select 'ABC',COL_B from TABLE_A WHERE COL_B=CONDITION`... (6 Replies)
Discussion started by: dinesh1985
6 Replies

9. Shell Programming and Scripting

Shell script to remove duplicates lines in a file

Hi, I am writing a shell script that needs to remove duplicate lines within a file by category. example: section a a c b a section b a b a c I need to remove the duplicates within th category with out removing the duplicates from the 2 different sections (one of the a's in section... (1 Reply)
Discussion started by: RichElks
1 Replies

10. Shell Programming and Scripting

shell script to remove old files and write to a log file

Hi, I have a script that works on a unix box but am trying to get it working on a linux box that uses shell. I am not a programmer so this is proving harder than I imagined. I made some changes and ended up with the script below but when I run it I get the following messages. Any help would be... (4 Replies)
Discussion started by: yabai
4 Replies
Login or Register to Ask a Question