LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2013, 03:19 PM   #1
cmovva
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
How to list files when connected to another server using SFTP in an automated script


I have a .ksh file with the following code

#!/bin/ksh

USR='test'
HT='test.mycompany.com'
DIRNAME='/mydir/'

function LsFiles {
sftp $USR@$HT <<end_ftp
cd $DIRNAME
ls "-lrt *.pdf"
bye
end_ftp
}

LsFiles | grep "$@" | awk '{print $NF}' > list_of_files.txt

Questions
1. When I run the script it connects to the server but gives me following error message
Can't ls: "'/mydir/-lrt *.pdf" not found

How can I specify enter after cd $DIRNAME in an automated script?

2. What does grep "$@" do?

Thanks in advance
cmovva
 
Old 02-11-2013, 03:27 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,150

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
seems it would be easier to obtain a list of files using ssh then use scp to do the actual copying.

ssh (for system administration and running remote commands)
sftp (for an ftp like interactive prompt (read: babysitting))
scp (for automated file transfers).

i would also install sshfs for extra ease-of-use of drag-and-drop local file copy emulation.

also, are you sure the directory exists ? each command should be flushed automatically in linux (you shouldnt have to simulate hitting the enter key).

$@ prints out all the command line arguments

Last edited by schneidz; 02-11-2013 at 03:43 PM.
 
Old 02-11-2013, 03:43 PM   #3
cmovva
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for your quick reply. Could you please provide the sample syntax using scp in this case. I only need to copy based on ls "-lrt *.pdf"
 
Old 02-11-2013, 03:46 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Hi and welcome to LinuxQuestions!
Quote:
Originally Posted by cmovva View Post
1. When I run the script it connects to the server but gives me following error message
Can't ls: "'/mydir/-lrt *.pdf" not found
Remove the double quotes: they prevent filename expansion so that the asterisk is matched literally.
Quote:
Originally Posted by cmovva View Post
How can I specify enter after cd $DIRNAME in an automated script?
Not necessary. In standard input the newline acts as "enter".
Quote:
Originally Posted by cmovva View Post
2. What does grep "$@" do?
$@ is a special parameter that means all the arguments passed to the script on the command line. Look at the difference between $* and $@ here for more details.
Quote:
Originally Posted by cmovva View Post
Thanks in advance
cmovva
You're welcome!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get list of SSH users that are connected to server linux_3478 Linux - General 6 01-06-2010 09:10 AM
Automated script to DELETE files older than 2 days in a Particular folder siddhartha_ece2004 Linux - Newbie 14 07-11-2008 06:46 AM
[SOLVED] Find out number of users connected to my sftp server procfs Linux - Newbie 5 01-10-2008 11:47 PM
Automated FTP script to grab ONLY newest dated files kaiserbeto Linux - Newbie 4 10-20-2006 03:46 AM
Files truncated by sftp/sftp-server at 65kb gato Linux - Networking 1 12-18-2003 11:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration