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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to retrieve a file from specific path using unix script?
# 1  
Old 07-23-2011
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
Code:
#!/bin/ksh
PATH=  /usr/
if [ -d $PATH ]
then
  echo "is a directory"
  filename = $ls -ltr | tail -1
fi


Last edited by Franklin52; 07-23-2011 at 10:01 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-23-2011
???

It's impossible to understand what you want (for me at least).
# 3  
Old 07-23-2011
Don't use spaces around the "=" sign:
Code:
#!/bin/ksh
PATH=/usr/
if [ -d $PATH ]
then
  echo "is a directory"
  filename=$(ls -ltr | tail -1)
fi

# 4  
Old 07-23-2011
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/).

Code:
#!/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

Last edited by radoulov; 07-23-2011 at 12:06 PM.. Reason: Coda tags!
# 5  
Old 07-24-2011
MySQL

hi,

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


Code:
#!/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... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

2. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

3. Shell Programming and Scripting

how to retrieve the word between 2 specific words with shell script

Hi, I have a file content like: : : <span class="ColorRed"> 1.23</span><br> : : the value 1.23 will be changed from time to time, and I want to use a shell script command, e.g grep or sed, to retrieve only the value, how to do it? Thanks! Victor (6 Replies)
Discussion started by: victorcheung
6 Replies

4. Programming

How can i retrieve some specific lines from a file using C

Plz tel me how to retrieve some specific set of lines from a file and store it in a char buffer.I am seperating each record by ":" 22:abc:4 hardware:cd:xyz:2 hardware:eth:abc:6 hardware:mouse:xyz:3 hardware:ram:xyz:1 23:cde:3 hardware:cd:xyz:2 hardware:eth:abc:6 hardware:ram:xyz:1 ... (3 Replies)
Discussion started by: vigneshinbox
3 Replies

5. Shell Programming and Scripting

retrieve part of file path

Hi I am trying to use sed to retrieve part of my html file's path. I am having a hard time getting what I want. Could someone give me some help? I want to retrieve the section after html and before the file name For example if I have the following, ... (3 Replies)
Discussion started by: tiger66
3 Replies

6. Shell Programming and Scripting

Find a path of a specific file

Hi, A variable value is /home/samir/datas/data.txt I want /home/samir/datas. How can I get that. (11 Replies)
Discussion started by: samir_standing
11 Replies

7. Shell Programming and Scripting

how to retrieve required specific info from the file

Hi I have a file which consists of a number in the square brackets, followed by the blank line, then several lines which describe this number. This pattern is repeated several thousands time. The number in the brackets and the decription of it is unique. For example: ASRVSERV=1241GD;... (2 Replies)
Discussion started by: aoussenko
2 Replies

8. Shell Programming and Scripting

how do we retrieve a line from a file in unix

we need to capture a record from a file in to a variable and do modifications to it .. so capturing line by line in a file in to some variable (2 Replies)
Discussion started by: lmadhuri
2 Replies

9. UNIX for Dummies Questions & Answers

(cont) Retrieve line from a file based on a value in specific column

HI, Your help was great: awk -F":" '$5 ~ /^P/{print }' file I would like to know what changes need to be done to this line code, so that I can put it in a shell script and call it as the example below. example: countries that start with chacater 'P' > country P Result: ... (0 Replies)
Discussion started by: efernandes
0 Replies

10. UNIX for Dummies Questions & Answers

Retrieve line from a file based on a value in specific column

Hi, I have a file that has several values seperated by ":" 2006:John:Student:Football:Portugal:Cinema 2006:James:Engineer:Basket:Poland:Theatre 2007:Lucy:Diver:Gymnastic:England:Music 2007:Smith:Plumber:Basket:Spain:Poker I need make a filter based on the 5th field to find countries that... (1 Reply)
Discussion started by: efernandes
1 Replies
Login or Register to Ask a Question