Script to keep only 2 or 1 file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to keep only 2 or 1 file
# 1  
Old 03-24-2006
Script to keep only 2 or 1 file

I have script that running for importing csv to mysql. It started by unzip file.zip. After complete the process from unzip until importing. I will have to rm the import.csv and backup the file.zip ( combined with date ) to /zip_backup/file.zip.date.

example:
/backup/
file.zip.240306
file.zip.230306
.
.

Because of the file.zip is big...so i plan to add more function in my script either one of below:

1- the next time script running, it will remove the previous backup or
2- only keep 2 file by latest date. [example: assume today is 240306. the existing backup is file.zip.230306 and file.zip.220306...so when i want to run the script for today, at the end script will remove the file.zip.220306 and only keep file.zip.240306 and file.zip.230306

####Part of my script##########

DATE="`date '+%d%m%y'`"
filename="file.zip"

## after successfull, current file.zip will be transfer to zip_backup/
mv -f "[command to import]" "/usr/local/apache/htdocs/website/zip_backup/$filename.$DATE"

###########################

As part of my code above, the script transfer to backup by file.zip.date. but the problem is my script doesn't have function to do either no1. or no2. as above mention. Please guide me.

Thank you
# 2  
Old 03-24-2006
Do you want the older file deletion...u can achieve it by ctime of find command..
find /usr/local/apache/htdocs/website/zip_backup/ -name -ctime +2 -exec rm {} \;
This will remove the files which was changed 2 days ago...but because it will delete all the files in directory..in case u want to be specific then u will have to use -name option too.
# 3  
Old 03-24-2006
yes i want to backup only latest 2 files....the file.zip.$date is not daily backup.

example:

file.zip.240306 <- keep
file.zip.200306 <- keep
file.zip.130306 <- remove
# 4  
Old 03-24-2006
ls -t file.zip.*
will produce a list of all file.zip files in order by modification timestamp. Now delete the top two items from that list...

ls -t file.zip.* | sed 1,2d
will produce a list of file.zip files in order by modfication timestamp, but without the two newest files.

finished product....
Code:
#! /usr/bin/ksh
ls -t file.zip.* | sed 1,2d | while read file ; do
         rm $file
done

# 5  
Old 03-24-2006
mind me asking. the "while read file"...the word 'file' is not same as 'file'.zip right?

As i already declare in my script:

#/bin/ksh

file="file.zip"

#######then insert this command#######

#! /usr/bin/ksh
ls -t "/directoryofthebackupfile/file.zip.*" | sed 1,2d | while read file ; do
rm $file
done

###### end ######################

correct or not?
# 6  
Old 03-25-2006
It looked to me like you used filename rather than file. But if file is in use, pick another variable. Don't put quotes around the file pattern.
# 7  
Old 03-26-2006
My Script:
#!/bin/bash
filename="/tmp/file.zip"
toimport="/usr/local/bin/php -q /path/import.php"
LOGTO=/var/log/myscript.log

if [ -s "$filename" ]; then
unzip "$filename" -d /tmp #-d tells where to unzip
[ -s "/tmp/file.csv" ] || { echo "`date`: file.csv not found in the zipfile" >> $LOGTO; exit 1; };
echo "`date`: file.csv unzipped OK `wc -c /tmp/file.csv`" >> $LOGTO
if $toimport; then
echo "`date`: file.csv imported OK" >> $LOGTO
rm -f "$filename" /tmp/file.csv
ls -t "/tmp/$filename.*" | sed 1,2d | while read $filename ; do rm $filename
else
echo "`date`: file.csv import FAILED" >> $LOGTO
fi
else
echo "`date`: $filename not found" >> $LOGTO;
exit 1;
fi


And I got this error:

[root@mydomain test]# sh testing.sh
testing.sh: line 22: syntax error near unexpected token `else'
testing.sh: line 22: `else'
[root@mydomain test]#
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

3. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

4. Shell Programming and Scripting

Need output of script on screen and file with correct return status of the called script.

Hi, I am trying to capture logs of the script in the file as well as on the screen. I have used exec and tee command for this. While using exec command I am getting the correct output in the file but, script output is not getting displayed on the screen as it get executed. Below is my sample... (14 Replies)
Discussion started by: Prathmesh
14 Replies

5. Shell Programming and Scripting

Call a Perl script within a bash script and store the ouput in a .txt file

I'm attempting to write a bash script that will create a network between virtual machines. It accepts three arguments: an RSpec that describes the network topology, and two list of machines (servers and clients). I have a (working) Perl script that I want to call. This Perl script takes an RSpec... (6 Replies)
Discussion started by: mecaka
6 Replies

6. Shell Programming and Scripting

Script to call a menu script and redirect each option to a text file

Hello, I want to design a script that will call an existing menu script and select options one by one and redirict the out put to a file. For example;- In the script MENU.sh there are 10 options i want to design a script MENU2.sh that will select option 2 3 4 6 7 10 and redirict the output... (4 Replies)
Discussion started by: spradha
4 Replies

7. Shell Programming and Scripting

Script to read a log file and run 2nd script if the dates match

# cat /tmp/checkdate.log SQL*Plus: Release 11.2.0.1.0 Production on Mon Sep 17 22:49:00 2012 Copyright (c) 1982, 2009, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production FIRST_TIME NEXT_TIME... (1 Reply)
Discussion started by: SarwalR
1 Replies

8. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

9. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question