The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to write a script in UNIX to find a file if another file exists mmdawg Shell Programming and Scripting 1 05-04-2008 07:40 PM
unix script to takes the old data from a TXT file and compress them into new file vpandey Shell Programming and Scripting 2 03-05-2008 08:10 AM
Shell Script to Load data into the database using a .csv file and .ctl file Csmani Shell Programming and Scripting 3 05-24-2006 05:09 AM
how to find Script file location inside script asami Shell Programming and Scripting 10 03-14-2006 09:57 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 02:25 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-24-2006
Registered User
 

Join Date: Mar 2006
Posts: 12
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
Reply With Quote
Forum Sponsor
  #2  
Old 03-24-2006
Registered User
 

Join Date: Mar 2006
Location: Gurgaon
Posts: 26
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.
Reply With Quote
  #3  
Old 03-24-2006
Registered User
 

Join Date: Mar 2006
Posts: 12
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
Reply With Quote
  #4  
Old 03-24-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,656
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
Reply With Quote
  #5  
Old 03-24-2006
Registered User
 

Join Date: Mar 2006
Posts: 12
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?
Reply With Quote
  #6  
Old 03-24-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,656
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.
Reply With Quote
  #7  
Old 03-26-2006
Registered User
 

Join Date: Mar 2006
Posts: 12
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]#
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0