![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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]# |
|||
| Google The UNIX and Linux Forums |