Want to mv files form one folder to another by date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to mv files form one folder to another by date
# 1  
Old 04-08-2008
Want to mv files form one folder to another by date

Hi All

Looking for some help here I want to query a file and mv all files that are older than seven days to an archive folder.

the archive folder already exists but I am having trouble with the date comparison.

list file is as follows

eg

dd2302071334.txt
dd2302071358.txt
dd2303061655.txt
dd2306061111.txt
dd2308061147.txt
dd2308061422.txt
dd2308061427.txt
dd2308071447.txt
dd2308071546.txt
dd2401081059.txt
dd2401081131.txt
dd2405061224.txt
dd2405071055.txt
dd2405071448.txt

I am trying to mv everything older than seven days, this will be run on a cron every night.

Cheers in advance

SummitElse
# 2  
Old 04-08-2008
got some code but stuck for the comparison part

ls | {

while read x;
do
y = $(echo $x | cut -c3-8)

need to compare the output with the computer date

and if older than seven days move it as below

if [! -f $y];
then
echo "moving $x to $y" >> $ARCHIVE
mv $x $y 2>> $ARCHIVE
fi
done
}

any help appreciated.

Last edited by SummitElse; 04-08-2008 at 09:39 AM..
# 3  
Old 04-08-2008
Code:
find . -mtime +7 -exec mv {} dest_folder \;

Just the find command with its built-in exec function.

The {} stands in for whatever results come from find, and you have to use the \; to terminate the command.
# 4  
Old 04-09-2008
Query

Just a quick query does that work on the datestamp of the file and not the actual date format in the file name, I have created the copies of these files from one server to the other so the datestamp will be the same on all of them
# 5  
Old 04-09-2008
GNUawk
Code:
awk  'BEGIN { 
 ## you do the maths...this may not be correct. calculate how many secs in 7 days
 onehr=60
 onemin=60
 oneday = ( onehr * onemin ) * 24
 sevendays = oneday * 7  
}
 day=substr($0,3,2)
 month=substr($0,5,2)
 year=substr($0,7,2)
 hr=substr($0,9,2)
 min=substr($0,11,2)
 t="20"year" "month" "day" "hr" "min" "00
 d=mktime(t)
 now=systime()
 if ( ( now - d ) >= sevendays ) {
  print $0
 }
}' file

# 6  
Old 04-09-2008
Try this code:
Code:
#!/bin/ksh

GetDate()
{
# Exemple: GetDate -1 '+%Y%m%d'

typeset -i nDays=$1; format=$2
eval $(echo $TZ | sed '
s!\([^-0-9]*\)\([-0-9]*\)\(.*\)!typeset -i localOffset=\2;zon1=\1;zon2=\3!')
TZ=$zon1$((localOffset-24*nDays))$zon2 date $format
}

liste=$(ls dd*.txt)

ndate=$(GetDate -7 '+%y%m%d')

for i in $liste
do
        # Get the file date in YYMMDD format

        ydate=$(echo $i | cut -c7-8)
        mdate=$(echo $i | cut -c5-6)
        ddate=$(echo $i | cut -c3-4)

        zdate=${ydate}${mdate}${ddate}

        if [ $zdate -lt $ndate ]
        then
                echo "$i: file to move"
        else
                echo "$i: do not move"
        fi
done

Good luck ! SmilieSmilie
# 7  
Old 04-09-2008
Cheers V310

That worked a treat mate you truly are a scripting guru, thanks again mate. think I'll need to study up on SED
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

2. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

3. UNIX for Dummies Questions & Answers

Select all files in a folder based on creation date (ls command)

Hi All, <Re-posting in Correct group> I'm trying to select all the files in a folder that starts with a particular name format and are created in a gven date range using 'ls' command...but i'm not successful.... Example : I'm trying to see all the text files in a folder who names start... (6 Replies)
Discussion started by: Satya C1
6 Replies

4. HP-UX

How can I find the size of files added to a folder after a particular date

Hi, I want to find the size of the files added to a folder after a certain date(say 1st of october), I know we can list the files which were created after a certain date , but is there anyway to find the total size of those files ? (3 Replies)
Discussion started by: alookachaloo
3 Replies

5. Shell Programming and Scripting

ksh shell script to add date (YYYYMMDDHHMISS) to all .txt files in a folder

Everyday 15 files are written to a folder \app\where\thefiles\are\destined\CURRFOLDER Task1: I need to add date in YYYYMMDDHHMISS format to each of them. Example: File: ACCOUNT.txt Should be updated as: ACCOUNT_20101005175059.txt Task 2: After I update the files, they need to be ... (2 Replies)
Discussion started by: Duminix
2 Replies

6. Linux

rename files in a folder with date&time stamp

Hi, I want to rename all the files (more than 100 files) in a fodler to another folder with date&time stamp. foe eg, file1.dat file2.dat file3.dat .. to be renamed as file1100629_16_30_15.txt (yy-mon-dd_hh_mi_ss) file1100629_16_30_16.txt .. so on (2 Replies)
Discussion started by: feroz
2 Replies

7. Shell Programming and Scripting

Transfer files with web based form by date

Not sure how I should approach this one. I have server X and Server Y. X is a collector. All files are seperated by hour. Y is used strictly for analysis. I do not always need all files from X. Sometimes other people use Y for analysis and do not always know how to transfer the files from... (3 Replies)
Discussion started by: mrlayance
3 Replies

8. Shell Programming and Scripting

command to list files with path & date in a folder

Hi, I need command to display files with full path and date of files where are generated at every 5hrs in a folder. eg: /u01/app/test/orjthsd_1_1 Sun May 10 19:03:26 2009 /u01/app/test/weoiusd_1_1 Sun May 10 21:00:26 2009 thanks saha (3 Replies)
Discussion started by: saha
3 Replies

9. UNIX for Dummies Questions & Answers

How can i delete files in folder by date?

Hi, I have some files on a folder and i want to delete all the files that were created on July. Thanks, Kobi. (9 Replies)
Discussion started by: kobibn
9 Replies
Login or Register to Ask a Question