1748134 Members
3353 Online
108758 Solutions
New Discussion юеВ

Re: Touch the files

 
viseshu
Frequent Advisor

Touch the files

When i give ll -t , i will get the most recent files.
I need to change the timestamp of particular filetype to that of the oldest file ( actually 2min later than the oldest file)
for example:
if i hav 2 files: file1,file2
file1 is having time stamp of 12:00
file2 is having time stamp of 9:00

i need the time stamp of file1 to be changed to 9:02


pls help
8 REPLIES 8
Peter Godron
Honored Contributor

Re: Touch the files

Hi,
see "man touch"
touch -t 200702190902 file1
viseshu
Frequent Advisor

Re: Touch the files

Thanks for the help!! but the time i have mentioned is just an example. Can you please put it in generic terms...Herez the description again

I need to touch the recent file in a path with a time stamp, 2 minutes newer than the older file.
Peter Godron
Honored Contributor

Re: Touch the files

Hi,

#!/usr/bin/sh
file=`ls -t1 $1| tail -1`
file2=`ls -t1 $1| tail -2 | head -1`
ar -r foo $1/$file
timer=`tail -1 foo | awk -F' ' '{print $2}'`
echo "Original file $file " `echo "0d$timer=Y" | adb | tr -d '\011'`
timer=`expr $timer + 120`
newdate=`echo "0d$timer=Y" | adb | tr -d '\011' `
year=`echo $newdate | awk '{print $1}'`
month=`echo $newdate | awk '{print $2}'`
day=`echo $newdate | awk '{print $3}'`
time=`echo $newdate | awk '{print $4}'`
hour=`echo $time | awk -F':' '{print $1}'`
min=`echo $time | awk -F':' '{print $2}'`

typeset -u mmm=`echo $month`
typeset -u months=`cal $year | grep "[A-Z][a-z][a-z]"`
i=1
for mon in $months
do
if [ "$mon" != "$mmm" ]
then
i=`expr $i + 1`
else
typeset -xZ2 i
month=$i
fi
done
echo "touch -t $year$month$day$hour$min $1/$file2"

Assuming your files are in ./test
$ ll ./test
total 0
-rw-rw-rw- 1 xxxxxxxx users 0 Feb 20 12:00 file1
-rw-rw-rw- 1 xxxxxxxx users 0 Feb 20 09:00 file2
$

Running:
./a.sh ./test
Original file file2 2007 Feb 20 09:00:00
touch -t 200702200902 ./test/file1
$

Have you thought about reversing the date listing with ls -lrt ?

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.


James R. Ferguson
Acclaimed Contributor

Re: Touch the files

Hi:

# perl -wle '$f1=shift;$f2=shift;$t=(stat($f1))[9]-120;utime($t,$t,$f2)' file1 file2

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Touch the files

Hi (again):

Oops, you want 'file' to be timestamped 2-minutes *later* than 'file2', so:

# perl -wle '$f1=shift;$f2=shift;$t=(stat($f2))[9]+120;utime($t,$t,$f1)' file1 file2

Regards!

...JRF...
Peter Godron
Honored Contributor

Re: Touch the files

Viseshu,
I assume this problem is now resolved ?

Could you please complete the thread by awarding points to helpful answers and summarising the solution for you.

This will help resolution of similar problems in the future.
Pavitra
Occasional Advisor

Re: Touch the files

ls -rt $PATH|head -1|read FILE
then if i give
touch -r FILE FILE1
the timestamp will be changed..


But i dont want this FILE as reference. i want its timestamp to be taken as reference since while touchin the file, the reference file may b moved by some other job..
pls help
Dennis Handly
Acclaimed Contributor

Re: Touch the files

>Pavitra: But i dont want this FILE as reference.

(Are you somehow the author too?)

If you want it 2 minutes later, you can't use touch -r.

>i want its timestamp to be taken as reference since while touching the file, the reference file may be moved by some other job

This can happen in any of the above scenarios. You will have to legislate that you can't have another job, or the window is so small it won't matter. With solutions by Peter and JRF, the window will be very small, after your reference file is found. Of course if you have to change the time on N files, you may have to create a dummy reference file to use for the N other files.