How to copy specific file.txt in specific folder?

hye there...
i have a problem to copy file in specific folder
that will change the name according to
host,time(%m%s) and date(%Y%M%D)
example folder name:
host_20100531.154101801

this folder name will always change...
but i just want to copy the AAA.txt and BBB.txt file..

really need ur help...
thanks...

I am unclear about the time format.

You can do something like:

cp AAA.txt host_$(date +'%Y%m%d.%m%s')

Change the date thing as you need.

i want to run it automatically...
what should i do...

Put the command in a script. further if you want to execute it automatically too, schedule it in cron.
Not sure if I understood you clearly.

Try this

cp AAA.txt BBB.txt host_????????.?????????

i understand what did you mean...
but then i just want the latest file..

latest file is mean lates directory named host_* ?

yup...
sori for the mistake...
its latest folder...

 touch -t $(date +%Y%m%d%H%M) tmpfile
 mydir=$( (find . -name "host_????????.?????????*" -type d -newer tmpfile) )

if [$? -ne 0 ] ; then

 cp AAA.txt BBB.txt $mydir
fi

mydir=$( (ls -1dt host_????????.????????? | head -n 1) )
cp AAA.txt BBB.txt $mydir

this is some of the file that i will received.
10.0.1.79_20100601.085868276
10.0.1.79_20100601.090022717
10.0.1.79_20100601.090108699
10.0.1.79_20100601.090149586

but i will continuously get new file like above.
inside the directory i only have to copy two specific file.txt
the problem is i have to copy only the latest file.txt from the latest directory i received..

need ur help ASAP :confused:

Please, excuse the interruption. I'm trying to follow this thread, but really I have no idea what you are asking for.

These are files, or directories?:

10.0.1.79_20100601.085868276
10.0.1.79_20100601.090022717 
10.0.1.79_20100601.090108699 
10.0.1.79_20100601.090149586

This suggests they are files:

So what does this mean:

What directory?

And this:

What are the files here? What are the directories?

Maybe you should try to start again, from the beginning - slowly, and more clearly, because this is really going nowhere, sadly.

sorry for making you confused.

1)these are the directories:
10.0.1.79_20100601.085868276
10.0.1.79_20100601.090022717
10.0.1.79_20100601.090108699
10.0.1.79_20100601.090149586

2)yup i will continuously get new directory like above
but the directory name according to time and date that they are created.

3)inside these the latest directory that have been created.
can you get what i mean.
if not please ask me.
sorry for that.
and thanks for the reply...

So, there is nothing like "latest file" i guess.
you have a file, named "file.txt" which is available in perhaps all the directory. and you want to copy "file.txt" from the latest directory.

one question,

10.0.1.79_20100601.085868276

The green part is millisecond or something?

ls -lrt 10.0.1.79*

Check if this gives you the latest directory ( which you want ) in the last.
if not, I think we need to sort the directories based on the date/time present in the directory name to get the latest directory.

yup...
you are right...:slight_smile:

but then i do not sure whether
10.0.1.79_20100601.085868276green part is millisecond or not.

i want it based on time and date.
thanks

So, do you mean that green part can be ignored?
Its a part of time stamp afterall.please do confirm this.

Also, What about the second part ? you didn't answer that!

no..it cannot be ignored...

sorry for not answering your question...
ls -lrt 10.0.1.79*this is the output:
10.0.1.79_20100601.090108699
total 24
10.0.1.79_20100601.090149586
total 24
10.0.1.79_20100601.090022717
total 27804
10.0.1.79_20100601.085868276
total 30740

i don't think the latest one is the last.
i think it based on the total.

---------- Post updated at 07:40 AM ---------- Previous update was at 07:30 AM ----------

the directory name is based on :
host +"_"+ Time.now.strftime(%Y%m%d.%M%S")+sprintf("%.5d",rand(100000))

you are using random number for last five digits.

try this if it works:

ls -d 10.0.1.79* | sort -t"_" -nk2.1,2.4 -k2.5,2.6 -k2.7,2.8 -k2.10,2.11 -k2.12,2.13

this will try to sort the file based in the date/time present in the dir-name.
here the last five digit (random numbers) has been ignored.

still first try only below and check for the last dir name.

ls -lrtd 10.0.1.79* 

thanks for detail info.

ls -d 10.0.1.79* | sort -t"_" -nk2.1,2.4 -k2.5,2.6 -k2.7,2.8 -k2.10,2.11 -k2.12,2.13
yup this is works.
it display the latest directory.

ls -lrtd 10.0.1.79*
the output are:
10.0.1.79_20100601.090108699
10.0.1.79_20100601.090149586
10.0.1.79_20100601.090022717
10.0.1.79_20100601.085868276

thanks

---------- Post updated at 10:55 AM ---------- Previous update was at 10:51 AM ----------

i don't understand this:
-nk2.1,2.4 -k2.5,2.6 -k2.7,2.8 -k2.10,2.11 -k2.12,2.13

can you tell me what is this mean.

---------- Post updated at 11:50 AM ---------- Previous update was at 10:55 AM ----------

what should i do.
if i want to copy file.txt in the largest total mount of directory?