How to retrieve a file from specific path using unix script?

Hi i'm new to shell script,
i want to get the filename from specific location which i mentioned in my script.
The scirpt should read the filename exactly using the following command "ls -ltr | tail -1".

Could someone show me on this. Below is my script

#!/bin/ksh
PATH=  /usr/
if [ -d $PATH ]
then
  echo "is a directory"
  filename = $ls -ltr | tail -1
fi

???

It's impossible to understand what you want (for me at least).

Don't use spaces around the "=" sign:

#!/bin/ksh
PATH=/usr/
if [ -d $PATH ]
then
  echo "is a directory"
  filename=$(ls -ltr | tail -1)
fi

Hi,

Thanks for your reply.

What i exactly required is, i need to write a shell script which list the latest file from the path (ex: /www/a/temp/in/) and i want to reterive the contents of that latest file into a variable defined inside my shell script. (ex: filename).

I'm running my script in the location (/www/a/).

#!/bin/ksh
PATH= /www/a/temp/in/
if [ -d $PATH ]
then
#it should copy the contents of latest file in the specified path into a variable, say for example filename
fi

Please help me on this.

Thanks for your help in advance

hi,

You have specified your target directory different from the directory in which script is present...

#!/bin/ksh
PATH=/full/path/to/your/target/directory
cd $PATH   #go in that directory
filename=$( ls -lrt | tail -1 | awk '{print $7}') #variable contains name of latest file
file_content=$(cat $filename) #this variable contains content of that file...
echo $file_content #to see the output

#your code.....=>
if [ -d $PATH ]    # I didn't understand your purpose of checking the directory here...
then
  echo "is a directory"
  filename=$(ls -ltr | tail -1)
fi

Regards,
ironmonkey... :slight_smile: