Script to keep todays files based on Timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to keep todays files based on Timestamp
# 1  
Old 09-07-2015
Script to keep todays files based on Timestamp

Hi i need to keep todays files based on timestamp and archive the remaining files

ex:

Code:
Managerial_Country_PRD_20150907.csv
Managerial_Country_PRD_20150907.csv
Managerial_Country_PRD_20150906.csv
 Managerial_Country_PRD_20150905.csv


Last edited by rbatte1; 09-09-2015 at 06:22 AM.. Reason: Added CODE tags for file list
# 2  
Old 09-07-2015
What - except for double posting - have you tried so far?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-07-2015
Hello ram1228,

Following may help you in same.
Code:
DATE=`date +%Y%m%d`
for i in *.csv
do
    if [[ $i == "Managerial_Country_PRD_"$DATE".csv" ]]
    then
          B=1
    else
          echo "mv $i"
    fi
done

You can put it into a script and execute it. Once you are happy with results you can remove echo above.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 09-07-2015
Thanks Ravinder..

IF I have two files with different names like

Code:
 Physical_Location_PRD_20150907.csv
 Managerial_Country_PRD_20150907.csv
Physical_Location_PRD_20150906.csv
 Managerial_Country_PRD_20150906.csv

then I want to keep todays files....

Last edited by rbatte1; 09-07-2015 at 10:14 AM.. Reason: Added CODE tags for file list to show leading space
# 5  
Old 09-07-2015
Hello Ram,

You could try following code then, may help you. Let me know if you have any queries.
Code:
DATE=`date +%Y%m%d`
for i in *.csv
do
    if [[ $i == *$DATE".csv" ]]
    then
          B=1
    else
          echo "mv $i"
    fi
done

EDIT: You can use as follows toofind command.
Code:
DATE=`date +%Y%m%d`
find -type f -iname "*.csv" ! \( -name "*$DATE.csv" \) 2>/dev/null

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-07-2015 at 08:12 AM.. Reason: Added a solution with find command too now
# 6  
Old 09-07-2015
Hi Rudic,

I have tried with -mtime , but never worked on timestamp in file .

Last edited by rbatte1; 09-07-2015 at 10:13 AM.. Reason: Added ICODE tags
# 7  
Old 09-07-2015
-mtime in what context?
Howsoever, did RavinderSingh13's proposal solve your problem? Then please tag the thread "solved". If not, please describe in detail WHAT is missing.

---------- Post updated at 19:23 ---------- Previous update was at 19:17 ----------

BTW, you might want to modify the name/header of your thread to make it more readable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script to keep todays files based on Timestamp

Hi i need to keep todays files based on timestamp and archive the remaining files ex: Managerial_Country_PRD_20150907.csv Managerial_Country_PRD_20150906.csv Managerial_Country_PRD_20150905.csv (2 Replies)
Discussion started by: ram1228
2 Replies

2. UNIX for Dummies Questions & Answers

Display files based on particular file timestamp

Hi, I have requirement to list out files that are created after particular file. ex. I have below files in my directory. I want to display files created after /dirdat/CG1/cg004440 file. ./dirdat/CG1/cg004438 09/07/14 0:44:05 ./dirdat/CG1/cg004439 09/07/14 6:01:48 ... (3 Replies)
Discussion started by: tmalik79
3 Replies

3. Shell Programming and Scripting

Find and copy files based on todays date and search for a particular string

Hi All, I am new to shell srcipting. Problem : I need to write a script which copy the log files from /prod/logs directory based on todays date like (Jul 17) and place it to /home/hzjnr0 directory and then search the copied logfiles for the string "@ending successfully on Thu Jul 17". If... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

4. Shell Programming and Scripting

bash script to create txt files based on timestamp

Hi , please guide me for a bash script that will create a txt files and the name of the txt files will be as of timestamp so that each file name will be different from other and these files will be get created say after every 10 minutes in a folder(/home/p2000/sxs137), please guide me how would... (1 Reply)
Discussion started by: nks342
1 Replies

5. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

6. UNIX for Dummies Questions & Answers

How to pick only the latest files based on the timestamp?

I have a few log files which get generated on a daily basis..So, I need to pick only the ones which get generated for that particular day. -rw-r--r-- 1 staff 510732676 Apr 7 22:01 test.log040711 -rwxrwxrwx 1 staff 2147482545 Apr 7 21:30 test.log.2 -rwxrwxrwx 1 staff 2147482581 Apr 7 19:26... (43 Replies)
Discussion started by: win4luv
43 Replies

7. Shell Programming and Scripting

copy files based on creation timestamp

Dear friends.. I have the below listing of files under a directory in unix -rw-r--r-- 1 abc abc 263349631 Jun 1 11:18 CDLD_20110603032055.xml -rw-r--r-- 1 abc abc 267918241 Jun 1 11:21 CDLD_20110603032104.xml -rw-r--r-- 1 abc abc 257672513 Jun 3 10:41... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

8. Shell Programming and Scripting

Shell script help to eliminate files of todays date

Hi I am very new to shell scripting and have written a script (below). However the directory I am searching will contain a file with a .trn extension each day which I want to eliminate. Each day the file extension overnight will change to trx, if this fails I want to know. Basically what I... (2 Replies)
Discussion started by: richM
2 Replies

9. Shell Programming and Scripting

Purge files based on timestamp avl in file name

Dear All, I have the followoing requirement.. REQ-1: Suppose I have the following files XX_20070202000101.zip XX_20080223000101.zip XX_20080226000101.zip XX_20080227000101.zip XX_20080228000101.zip XX_20080229000101.zip Suppose sysdate = 29 Feb 2007 I need to delete all files... (3 Replies)
Discussion started by: sureshg_sampat
3 Replies

10. Shell Programming and Scripting

want to delete timestamp from todays file

i want to delete the time stamp from the file which have a date of yester day 640878 Nov 6 09:08 fbres.01.20031106:09:08:30 here is what my ls -lt command shows in current directory it want it to be 640878 Nov 6 09:08 fbres.01.20031106 thanks (5 Replies)
Discussion started by: maverick
5 Replies
Login or Register to Ask a Question