Find the latest file on remote sftp


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find the latest file on remote sftp
# 1  
Old 11-22-2012
Find the latest file on remote sftp

Hi All,

I need your help in finding the latest files in remote sftp and get those files to local server and process them. Please let me know

I appreciate your valuable inputs.

Thanks
raj
# 2  
Old 11-22-2012
try using this
Code:
ls -ltr | tail -1

this will gives you the last modified file in the directory, after which you can scp the file to your local server
try this script
Code:
#!/bin/bash
file=$(ls -ltr | tail -1)
if [ -f $file ] ; then
  scp [[user@]from-host:]source-file [[user@]to-host:][destination-file]
fi


Last edited by mukulverma2408; 11-22-2012 at 11:49 AM.. Reason: modification
# 3  
Old 11-22-2012
mukulverma2408, I think you got it a bit backwards, the files are on remote sftp, to be retrieved.

You can get a list of files this way I think, though you will need to check if ls -t works in your sftp. It works in mine, but sftp can vary, and does not run real shell commands.

Code:
sftp -b username@host <<EOF | grep -v "sftp>" | head > /tmp/$$
cd /path/to/folder
ls -t
EOF

# 4  
Old 11-22-2012
Code:
file=$(ls -ltr | tail -1)

it does not give exact filename, disaplays other information .....
Code:
file=$(ls -ltr | tail -1 | awk '{print $NF}')

will give the latest modified file name.
# 5  
Old 11-22-2012
Hi ,

Thanks for your replies. i tried Corona88's reply but that is not working. It says connecting to sftp but is not returning any values and not getting any files I need to get all the latest .csv files

How do I do it corona88's given code.

Thanks
raj
# 6  
Old 11-22-2012
Read my code, don't just blindly copy it. It connects to get the list of files and puts them in the /tmp/$$ file. I didn't see much point writing more until I knew whether your sftp would use ls -t.

So the next thing to do in your script is a loop reading from /tmp/$$.

Then once all is done, delete /tmp/$$.
# 7  
Old 11-22-2012
Try below steps:-

1. Create an sftp batch file:
Code:
cat > sftp_batch.cntrl
cd /to_you_directory/
ls -t *.csv

2. Run sftp using batch file:
Code:
sftp -b sftp_batch.cntrl user@host | grep -v "sftp>"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Retrieve the Latest file in a folder using SFTP

HI Guys, Can anyone help me to retrieve the latest file from below example using SFTP I have below list of files in a folder v403t.lstprgms.sortin1 val027.d099.fwest.oct2711 xcelrptd.d1400sqp.dec1713.t040459.@02del xcelrptd.d1400sqp.dec1713.t073308.@02del... (3 Replies)
Discussion started by: heye18
3 Replies

2. Shell Programming and Scripting

Need to automate sftp and always get the latest file

hi guys the script is working but i need to change the file that i want to get to the latest.. i will use this as cron so i can get the latest sftp file evryday. pls help.. set user "big_user" set IP "XX.XX.XX" set dir "/export/home/oracle/REF/INPUT" spawn sftp $user@$IP expect sftp> ... (7 Replies)
Discussion started by: JONATHAN0919
7 Replies

3. UNIX for Dummies Questions & Answers

SFTP to delete remote file

Hi guys In this scenario, i have been able to transfer the file from a remote server to my local server, but how to delete the file from remote server once its copied. I am using this command MYFILE=/tmp/MyFiLe.$$ echo rm FROM_DIR/K* >$MYFILE then sftp -b $MYFILE rm -f $MYFILE and I have... (1 Reply)
Discussion started by: rubel
1 Replies

4. Shell Programming and Scripting

How to FTP the latest file, based on date, from a remote server through a shell script?

How to FTP the latest file, based on date, from a remote server through a shell script? I have four files to be FTP'ed from remote server. They are of the following format. build1_runtime_mmddyyyy.txt build2_runtime_mmddyyyy.txt build3_runtime_mmddyyyy.txt buifile_count_mmddyyyy.txt ... (9 Replies)
Discussion started by: imran_affu
9 Replies

5. Shell Programming and Scripting

Need to copy latest file using SFTP

Hi All, In my unix server, I have the following files: h1.txt h2.txt h3.txt and through SFTP i need to copy only the latest file to another unix server. Can you please let me know what command i need to use. Thanks in Advance, (2 Replies)
Discussion started by: HemaV
2 Replies

6. Shell Programming and Scripting

SFTP latest file from Windows server to Unix host

Hi All, I am SFTP ing file from Windows server (Sydney) to Unix host in London (both servers are on different timezones). It appears the upstream (Windows server team) does not do a file housekeeping activity and henceforth there are multiple files i.e. each week the script is scheduled to run... (1 Reply)
Discussion started by: vigdmab
1 Replies

7. Shell Programming and Scripting

Latest file through SFTP

Hi, I want to download the latest file published on remote server using SFTP connection. The job will run at every 15 min hence should download only the latest file. The fileName will be like ABC_DDHHMISS.txt (DD- day, HH-hour, MI - Min, SS-Sec). So there will be files with same date for... (23 Replies)
Discussion started by: ssachins
23 Replies

8. Shell Programming and Scripting

copy the latest file in the remote server's directory

Hi Expert Team, I performed the below piece of code to copy the latest file in the remote server's directory to the same server's other directory. But it is not working properly. How can i handle this? Can you please help me..? ssh ${REMOTE_USERID}@${REMOTE_HOSTNAME} "cp -p `ssh... (3 Replies)
Discussion started by: spkandy
3 Replies

9. Red Hat

To find the LATEST file from a dir on REMOTE machine and SCP to local machine?

Hi All, URGENT - Please help me form a scipt for this: I need the LATEST file from a dir on REMOTE machine to be SCP'd to a dir on local machine. (and I need to execute this from local server) I know that the below cmd is used to find the LATEST file from a dir. But this command is not... (3 Replies)
Discussion started by: me_ub
3 Replies

10. Shell Programming and Scripting

Find and remove all but the latest file

Hi, Would appreciate if someone could help me with the following requirement. Say I have a directory where a file called abc_$timestamp.txt is created couple of times in a day. So this directory would have files like abc_2007-03-28-4-5-7.txt abc_2007-03-28-3-5-7.txt... (4 Replies)
Discussion started by: hyennah
4 Replies
Login or Register to Ask a Question