sftp - extract file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sftp - extract file name
# 1  
Old 08-09-2010
sftp - extract file name

Hi,

Here's the problem I'm facing. There's a common "sftp" script which list all the "done" files at the remote server and then "get" the corresponding data files.

Now because it has to be used by different programs the command "get" is constructed in the below manner:

Code:
 
get remote_path/remote_file1.csv* local_path
 
##The remote file that gets transferred is, say, remote_file1.csv_1000

Now I've to insert the exact remote file name in a table; but what is being inserted is "remote_file1.csv*". Is there a way to grab the exact file name (remote_file1.csv_1000) that gets transferred from remote to local path?

-dips
# 2  
Old 08-10-2010
I am not sure if I understand your needs - you could check with a dir command or ssh ls and get the names needed(?). If you have to take the exact file name after the file(s) has been transferred, you might check your local directory to get the filenames; maybe by date or whatever distinguishes them from "old" files.
# 3  
Old 08-10-2010
Try using mget instead of get
# 4  
Old 08-11-2010
Quote:
Originally Posted by rajamadhavan
Try using mget instead of get
I'm not sure how to use mget? How'll it provide me with the exact file name transferred from remote server to local server?

Quote:
Originally Posted by zaxxon
I am not sure if I understand your needs - you could check with a dir command or ssh ls and get the names needed(?). .
I cannot change code that makes use of "sftp -b <batchfile> user@host".

Quote:
Originally Posted by zaxxon
If you have to take the exact file name after the file(s) has been transferred, you might check your local directory to get the filenames; maybe by date or whatever distinguishes them from "old" files.
This will make the code more complex. As this script runs every hour and transfer all the files that are available at the remote location. So I'll have to incorporate intelligence to check how many files were transferred and then pick only that no. of files name from the local dir matching the current date & time(in minutes). I wanted something simple and simultaneous with the transfer of files.

So I came up with the following code:
Code:
echo "get remote_path/remote_file1.csv*.zip local_path" > <batch_file>
fetchFile=`sftp -b <batch_file> USER_ID@HOST`
fileNmPath=`echo $fetchFile | sed 's/sftp>//g' | awk '{print $NF}'`
fileName=`basename $fileNmPath`

-dips
# 5  
Old 08-11-2010
mget ist just a ftp command like get but it enables you to you use wildcards. It stands for multiple get.
But as you say you can not change any of the commands fed to the ftp client, this would not apply.
On the other hand looking at the code you posted you are in full charge of what you hand over your ftp client Smilie

Whatever - did your code work? Are you satisfied or is something still not as it should be?
# 6  
Old 08-11-2010
Quote:
Originally Posted by zaxxon
But as you say you can not change any of the commands fed to the ftp client, this would not apply.
On the other hand looking at the code you posted you are in full charge of what you hand over your ftp client Smilie
When I said I cannot change the code; what I meant was that I cannot alter the way ftp-ing is happening i.e. cannot change it to scp or ssh or mget, etc......but I can write few unix commands or sftp allowed commands to get what I want. The code is already in place which use sftp with -b (batchfile) option. Sorry for any confusion it caused.

Quote:
Originally Posted by zaxxon
Whatever - did your code work? Are you satisfied or is something still not as it should be?
Yes, its atleast working for me. But I'm not sure if its patching up or not? and if there is a neat way to do things.

-dips

Last edited by dips_ag; 08-11-2010 at 07:09 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Overwriting file with sftp

I have a script, which runs through cronjob every night 9 PM. It is supposed to do following tasks -- 1- Connect to ftp.testsite.com via user redtest 2- Fetch file red_bill.txt to my local server, where my script is residing. 3- Rename red_bill.txt to red_bill.V01.txt everyday on sftp server.... (4 Replies)
Discussion started by: solaris_1977
4 Replies

2. Shell Programming and Scripting

How to overwrite a file by transferring the file using sftp?

hi, i am using sftp to transfer files between two servers, if i connect to a remote server using sftp n moves a file say "S123.txt" to the remote server, it moves successfully, but when i try to execute the same sftp script to move the same files to the same remote server, the old file is... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

Sftp : not able to print the echo statements after the sftp transfer

I had the below sftp script working perfectly but the problem is I am not able to send the echo statements . #!/bin/sh echo "Starting to sftp..." sftp admin@myip << END_SCRIPT cd /remotepath/ lcd /localpath/ mget myfiles*.csv bye END_SCRIPT echo "Sftp successfully." echo echo... (11 Replies)
Discussion started by: scriptscript
11 Replies

4. Red Hat

Chroot sftp users, remote sftp login shows wrong timestamp on files

Hello, I have a weird issue, I have RHEL 5.7 running with openssh5.2 where sftpgroup OS group is chroot. I see the difference difference in timestamp on files, when I login via ssh and SFTP, I see four hour difference, is something missing in my configuration. #pwd... (8 Replies)
Discussion started by: bobby320
8 Replies

5. Shell Programming and Scripting

Renaming a file while using SFTP

Hi All, I have a script which works perfectly for using SFTP commands. I use SFTP -b batchfile remoteuser@remoteserver. This batch file contains a command cd /dir1 put file1 I am wondering if there is a flag to rename a file on Remote server. I see the option rename file1... (6 Replies)
Discussion started by: Hangman2
6 Replies

6. Shell Programming and Scripting

SFTP Process each File!

Hi All, I'm a newbie here in unix. I'm just wondering how can i process each file while in sftp? is it possible? Ex. server1 1.txt 2.txt how can i get the top of the file and process it. output: 1.txt is going to process because it's in the top. i can't figure out how to do it. T_T... (9 Replies)
Discussion started by: nikki1200
9 Replies

7. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

8. UNIX for Advanced & Expert Users

Copy a file with sftp

Hi, I should copy a file from my computer in folder on a server. I use sftp account@computername and insert the password. then I write sftp> put /home/path/file to copy the file in my accont folder but this command doesn't work and it says that it couldn't find the file. Hope I was... (5 Replies)
Discussion started by: jborges
5 Replies

9. Shell Programming and Scripting

Read file(s) and SFTP it

I have files starting with some char 'X' (they could vary from day to day) and I have to read all the files starting with char X*.txt and write to a single file Y.txt. But I have to omit the first line in each file (header) and consider only the header in first file i read. After all the... (4 Replies)
Discussion started by: nuthalapati
4 Replies

10. UNIX for Dummies Questions & Answers

sftp log file

Hi, I am running this command to sftp a file: sftp -o identityfile=/blah/blah/.ssh/key blah@mass019 > log.log I want to write the output to a log file, but for some reason when the sftp errors out the log file is empty. It seems that it writes to the log only if the connection is... (2 Replies)
Discussion started by: queenie680
2 Replies
Login or Register to Ask a Question