Page 1 of 1

Copying Content of a Newest file

Posted: Tue May 25, 2004 4:12 pm
by rkumar28
Hi,
I have a situation, I get ONE file everyday through FTP in my unix box. That file name varies depending on the source that is sending the file to us. I have created a generic file called LOAD.TXT file. DataStage points to this file. Everyday in Unix I copy the content of the newly ftped file to LOAD.TXT. I use this command cat ftped filename > LOAD.txt to copy the content.

I am trying to automate this process. I am trying to write a shell script that picks up the latest file in the directory(i.e. newest file that comes in) and pass the content of that file to LOAD.TXT. This script will be called by DataStage on the "Before job Routine".

I am little new to Unix and need some help/advice on the script. I tried something like:

var1=ls -1t | head -1 --> This line picks up the newest file in the directory.
cat $var1>LOAD.txt
I error out on the first line of my script.
Also I tried something like ls -1t | head -1 | cat >load.txt. But I am not getting the desired result.

Is their a way to pass the output of the line below to a variable var1.
ls -1t | head -1. Or Is their a better way to do through DataStage.

I will really appreciate any help in this regards.

Thanks
Raj

Posted: Tue May 25, 2004 4:31 pm
by ray.wurlod
Just make it a single command, using backquotes to capture the output of the pipeline.

Code: Select all

cat `ls -1t | head -1` > load.txt
Why cat? Surely a cp command would be more efficient.

Code: Select all

cp `ls -1t | head -1` load.txt
Maybe you want to remove load.txt first. No problem.

Code: Select all

rm -f load.txt ; cp `ls -1t | head -1` load.txt
You probably also want to cater for the case that there are no files in the directory, in which case the cp command would fail.

Code: Select all

rm -f load.txt ; ls -1t * && cp `ls -1t | head -1` load.txt

Posted: Wed May 26, 2004 7:16 am
by tonystark622
Raj,

I suspect that the first command is failing because you are doing a "long" format ls (ls -lt). Instead, try: ls -t . This should only give you the filename and not the information about the file.

Good Luck!

Tony

Posted: Wed May 26, 2004 7:22 am
by WoMaWil
Hi Tony,

Ray and Raj did it write because they used 1 (one) and not l (lambda).

Wolfgang

Posted: Wed May 26, 2004 7:24 am
by tonystark622
:oops: Oops. I'm embarassed. I didn't read that carefully. Thanks for the correction. :D

Tony