Copy files based on modification date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files based on modification date
# 1  
Old 06-02-2009
Bug Copy files based on modification date

How to copy files from a location to a directory <YYMM> based on last modification date? This will need to run daily.

I want to copy those file for May to 0905 and Jun to 0906.

Appreciate your guidance.Smilie Thanks.

-rw-rw-rw- 1 ttusr tgrp 4514 May 29 21:49 AB24279J.lot_a
-rw-rw-rw- 1 ttusr tgrp 4514 May 29 21:55 AB24279J_1.lot_a
-rw-rw-rw- 1 ttusr tgrp 1506921 Jun 1 20:40 AB24283J.hst_a
-rw-rw-rw- 1 ttusr tgrp 4283 Jun 1 20:40 AB24283J.lot_a
-rw-rw-rw- 1 ttusr tgrp 1506921 Jun 1 07:36 AB24285J.hst_a
# 2  
Old 06-02-2009
May not be robust but will resolve the issue. Assuming that all files are of the current year. Modify a bit to handle files created long back. Take care of the Paths , where you want to create the destination directory and all.

Code:
TESTING>cat mon.txt
Jan,01
Feb,02
Mar,03
Apr,04
May,05
Jun,06
Jul,07
Aug,08
Sep,09
Oct,10
Nov,11
Dec,12

script :

Code:
for i in `ls -l | grep "^-" |  awk '{ print $6 "," $9}'`
do
mon=`echo $i | awk -F"," '{print $1}'`
File=`echo $i | awk -F"," '{print $2}'`
moninnum=`awk -F"," -v mm=$mon '{ if ($1==mm) print $2}' mon.txt`

if [ -d "09"${moninnum} ];then
cp  -p  $File "09"${moninnum}
else
mkdir  "09"${moninnum}
cp -p $File "09"${moninnum}
fi
done

# 3  
Old 06-02-2009
use this
Code:
cp `ls -l|awk '$6 ~ /May/{print $9}'` somepath/0905
cp `ls -l|awk '$6 ~ /Jun/{print $9}'` somepath/0906

# 4  
Old 06-02-2009
Quote:
Originally Posted by vidyadhar85
use this
Code:
cp `ls -l|awk '$6 ~ /May/{print $9}'` somepath/0905
cp `ls -l|awk '$6 ~ /Jun/{print $9}'` somepath/0906

have to take care of files with spaces. if there are, then $9 will give ambiguous results.
also, no need to invoke cp + ls + awk 2 times
Code:
cp `ls ...... | awk $6~/May|Jun/ ....................` somepath......

# 5  
Old 06-02-2009
Quote:
Originally Posted by ghostdog74
also, no need to invoke cp + ls + awk 2 times
Code:
cp `ls ...... | awk $6~/May|Jun/ ....................` somepath......

Quote:
I want to copy those file for May to 0905 and Jun to 0906.
read the req fully Smilie
# 6  
Old 06-02-2009
Quote:
Originally Posted by vidyadhar85
read the req fully Smilie
Code:
ls -l | awk '$6 ~/May/{path="somepath/0905"}
$6~/Jun/{path="somepath/0906"}
{ cmd="cp "$9" "path; cmd|getline}'

# 7  
Old 06-02-2009
Quote:
Originally Posted by ghostdog74
Code:
ls -l | awk '$6 ~/May/{path="somepath/0905"}
$6~/Jun/{path="somepath/0906"}
{ cmd="cp "$9" "path; cmd|getline}'

one and the sameSmilie
nice trySmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort and move multiple files by modification date

Hi I have a problem, I have a large group of archive files in a folder some are later versions of the same archive, the only difference btween them is that the archiving program we use appends the name with a code for it to keep track of in its data base, and the modification date. I am starting... (6 Replies)
Discussion started by: Paul Walker
6 Replies

2. UNIX for Dummies Questions & Answers

Copy log based on from-date and to-date

Hi all, i go a customer support requirement where i need to scan several files based on from/to date like 1-oct to 2-oct please help... (3 Replies)
Discussion started by: AbhiJ
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

Move files from one directory to another based on creation/modification date

Hi All, Really stuck up with a requirement where I need to move a file (Lets say date_Employee.txt--the date will have different date values like 20120612/20120613 etc) from one directory to another based on creation/modification dates. While visiting couple of posts, i could see we can... (3 Replies)
Discussion started by: dsfreddie
3 Replies

5. Shell Programming and Scripting

Copy files based on last created date

Hi, I have a requirement to copy files from a windows network drive to a Linux server using shell script based on the last created date. Ex: FileName CreatedDate/Time F1 05-01-2012 3:00 PM F2 05-01-2012 3:15 PM F3 05-01-2012 2:00 PM When i run the shell script... (1 Reply)
Discussion started by: Lee_10
1 Replies

6. Shell Programming and Scripting

How to list the files based on the modification time using the find command?

Hi All, I need to list the files based modification time of the files from a directory, I cannot use "ls -t" as there are lot of files, which "ls" command cannot handle. New files will land there daily. So iam looking for an alternative through "find"command. All suggestions are welcomed. ... (6 Replies)
Discussion started by: Kesavan
6 Replies

7. Shell Programming and Scripting

Rename old files with last modification date

Hi everyone, I have files like file1_Mod.txt, file2_Mod.txt. I want to rename the old files with the last modification date. I write the below script to rename with current date, but I donīt know how to use "date -r" to get the last modification date with the same format I have below... (5 Replies)
Discussion started by: cgkmal
5 Replies

8. Shell Programming and Scripting

Copy files based on date

Hi all i am having so many files in my directory.Is there any option to copy files based on date. example i am having file this -rw-rw-r-- 1 ram user 1 Feb 2 17:12 abc -rw-rw-r-- 1 ram user 1 Feb 2 17:12 bnw -rwxrwxr-x 1 ram user 21122 Feb 4... (3 Replies)
Discussion started by: suryanarayana
3 Replies

9. UNIX for Advanced & Expert Users

Help with sorting files according to modification date

Hi, I was very surprised to not be able to find an answer to this question despite my best efforts in Google and elsewhere. Maybe it's a good thing as it forced me to finally become a member in this great forum that i use frequently. Ok my question: I want to be able to sort files inside a... (3 Replies)
Discussion started by: stavros
3 Replies

10. Shell Programming and Scripting

List the file or files with last modification date

hi. I need help my programing friends :p I need to list all the files with a certain name (for example FileName) by last modification date but only the one with the last date. If there are two files with the same name and same modification date it should print the both. For example in this set... (6 Replies)
Discussion started by: KitFisto
6 Replies
Login or Register to Ask a Question