Visit Our UNIX and Linux User Community


copy the latest file in the remote server's directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copy the latest file in the remote server's directory
# 1  
Old 08-10-2009
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..?

Code:
ssh ${REMOTE_USERID}@${REMOTE_HOSTNAME} "cp -p `ssh ${REMOTE_USERID}@${REMOTE_HOSTNAME} "ls -tr1 ${SOURCE_FILE} | tail -1"` ${TARGET_FILE}"

Thanks,
Kanda

Last edited by Yogesh Sawant; 08-10-2009 at 10:25 AM.. Reason: added code tags
# 2  
Old 08-10-2009
what error did you get?
# 3  
Old 08-11-2009
Hi Yogesh,

I didnt get any error, but the file is not copied to the target directory of the remote server.

Thanks,
Kanda
# 4  
Old 08-11-2009
Have you tried using scp instead of a nested ssh command? Here's an example below (without any ${} or {$})

Code:
### This is one line - broken down to improve readability
scp -rp REMOTE_USERID@REMOTE_HOSTNAME:SOURCE_FILE`ssh 
REMOTE_USERID@REMOTE_HOSTNAME ls -tr1 /SOURCE_FILE | tail -1` 
REMOTE_USERID@REMOTE_HOSTNAME:TARGET_FILE

I tested by running the following from myhost1:
Code:
scp -rp avronius@myhost2:/opt/home/avronius/misc/`ssh 
avronius@myhost2 ls -tr1 /opt/home/avronius/misc/ | tail -1` 
avronius@myhost2:/opt/home/avronius/misc/newfile


Previous Thread | Next Thread
Test Your Knowledge in Computers #941
Difficulty: Medium
A ping of death is a type of attack on a computer system that involves sending a malformed or otherwise malicious ping to a computer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get the latest file from a directory in Server B to Server A?

ssh user@hostnameB cd /home/oracle latest_file_target=$(ls -trp -I "*.pd.Sat" -I "*.pd.Lag" -R | grep -v "/.pd" | tail -1) scp -r $latest_file_target user@hostnameA:$TARGET_DIR/ exit (1 Reply)
Discussion started by: ahmed.vaghar
1 Replies

2. UNIX for Dummies Questions & Answers

Checking files in remote server and decide to copy file or not

Hi there, I have a problem in my script, I need to check whether file exists in remote server or not, if the file exists, then stop copy else copy the file to the server.. my code is something like this while read $server do if ssh $server "cd $directory_name; if ; then echo "Error:... (2 Replies)
Discussion started by: beezy
2 Replies

3. UNIX and Linux Applications

Need to copy the latest file from Unix server to Shared folder

Hi All, One job in unix server will generate .csv files daily. I need to copy the latest of these .csv file from the unix server to the shared drive/folder in windows through unix script. My shared folder will look something like W:\some folder(for example). Could any one of you please help... (3 Replies)
Discussion started by: jaya@123
3 Replies

4. Shell Programming and Scripting

Copy latest files from another server

Hi, I wanted to copy monthly generated csv file(s)( of latest date) at one server to my current server. So I wrote the following script, but does not work. The script is invoked on my current server Can you please help? !/bin/ksh #. /apps/comp/ cd /apps/di/dev/import/paper ssh hpqad02... (3 Replies)
Discussion started by: Alok Ranjan
3 Replies

5. 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

6. UNIX for Advanced & Expert Users

How to copy a file from remote server and preserve timestamp

Hi How to copy a file from remote server and preserve timestamp. Please not, i have to pass username and password to connect to the remote server in the shell script. Can this be achieved with simple ftp ? are there any options in ftp ? Thanks (4 Replies)
Discussion started by: skumar75
4 Replies

7. Shell Programming and Scripting

Copy the latest file to a directory

Hi Team, I wish to copy the latest file of pattern "MyFile*" to some other location. I need to do all the operation in a single command separated by |. ls -rt <MyFile*> | tail -1 | <copy command>. How can I do? Please help me. Thanks, Kanda (2 Replies)
Discussion started by: spkandy
2 Replies

8. Shell Programming and Scripting

Copy the latest file from one directory to another

Hi All, I am in the directory a/b/processed the files in this directories are -rw-r--r-- 1 owb users 330 Aug 8 chandantest.txt_08082008 -rw-r--r-- 1 owb users 220 Aug 7 chandantest.txt_07082008 -rw-r--r-- 1 owb users 330 Aug 6... (3 Replies)
Discussion started by: chandancsc
3 Replies

9. UNIX for Advanced & Expert Users

Commands to copy a tar.gz file from a Remote Unix Server to Local Desktop.

Hi, Just wanted to know, how can I ftp/transfer/copy a (design.tar.gz) archive from a Unix Server (sdmc222.sdmc.cp-srv.com) which is at a remote location, to my Windows Desktop. Obviously, it is not possible at cmd prompt on my Windows using the following commands :- ftp... (3 Replies)
Discussion started by: marconi
3 Replies

10. Shell Programming and Scripting

Copy a file on remote server

I have ssh keys setup and running properly between two servers. I have a Korn shell script that is logging into the remote server and needs to backup the authorized_keys and/or authorized_keys2 files. The following syntax works perfectly ------------------------------------- ssh... (1 Reply)
Discussion started by: sunsysadm2003
1 Replies

Featured Tech Videos