LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 02: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, 02:27 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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 02:43 PM.
 
Old 02-11-2013, 02: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, 02:46 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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



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 08:10 AM
Automated script to DELETE files older than 2 days in a Particular folder siddhartha_ece2004 Linux - Newbie 14 07-11-2008 05:46 AM
[SOLVED] Find out number of users connected to my sftp server procfs Linux - Newbie 5 01-10-2008 10:47 PM
Automated FTP script to grab ONLY newest dated files kaiserbeto Linux - Newbie 4 10-20-2006 02:46 AM
Files truncated by sftp/sftp-server at 65kb gato Linux - Networking 1 12-18-2003 10:29 AM

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

All times are GMT -5. The time now is 06:30 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
Open Source Consulting | Domain Registration