copying the latest file

Hi,

I have a problem.

I have some text files in a folder. The names can be like:

emp_20080307053015.dat
emp_20080306053015.dat
emp_20080305053015.dat
emp_20080304053015.dat

The date format appended is like yyyymmdd and timestamp.

What i need is i have to copy the latest file every day i.e. which has the latestdate appended to the file name to a different folder.

Can any one tell a shell script for this?

Thanks in advance....

If that folder only contains those files then you can simply use,

cp `ls -lrt|tail -1|awk '{print $9}'` <absolute path name of the folder>

It has limitations, but has got simplicity.

:slight_smile:

No this folder contains other files apart from these files also.
Can you please send me the script for this.

thanks.


today=" $(date '+%b %d') 
for file in $dir
do
if [[ ! -r $file || $(ls -ltr $file 2>/dev/null) = *$today* ]]; then
echo "File is today's file leave it"
else
echo "process something else"
fi
done

Thanks

the follwoing would work fine then,

cp `ls -lrt emp_*.dat|tail -1|awk '{print $9}'` <absolute path name of the folder>

cp `ls emp_*.dat | sort | tail -1` newfile

Thanks
Penchal

I tried using the cmd but it throws me the following error

ksh: awk{print $9}: not found
Usage: cp [-f|-i] [-p] [-S] [-e warn|force|ignore] source_file target_file
cp [-f|-i] [-p] [-S] [-e warn|force|ignore] source_file ... target_directory
cp [-f|-i] [-p] [-S] -R|-r [-e warn|force|ignore] source_directory ... target_directory

Thanks


latest_file=`ls -ltr | tail -1| awk '{print $9}'`

cp $latest_file <source_destination>

This is the simplest way you can do it.. This should not have any problems..

thank you its working fine when i am in the normal unix mode but when i login into a remote server thru ftp this command is not working..
Is there any other command that shud be used when u r in ftp mode.

Thanks.

If you are using ftp, use the fllowing commands:

recv remote-file [local-file]

This would Copy remote file to local PC

send local-file [remote-file]

This would copy local file to remote host.

recv remote-file [local-file]
how can we get the file name in the place of [local_file] in the above cmd.
do we have to use any other cmd for that if so pls let me know the cmd also.

thanks.

I guess you are not reading all the posts for this thread..

latest_file=`ls -ltr | tail -1| awk '{print $9}'`

This has the file you want to copy right..?

I tried using the same cmd in the ftp mode but it throws the foll error.
usage: ls remote-directory local-file

Thanks.