Ankur_kuls has asked for the wisdom of the Perl Monks concerning the following question:

I have one sftp script which first moves all the .seq files to temp folder then uses mget command and then deletes them.. below is the sftp portion of the script.

sshpass -p $pw sftp $userID@$serverIP <<EOF cd /root/perl rename *.seq /root/perl/temp/*.seq cd ./temp mget *.seq rm *seq bye EOF

now problem I am facing with rename command while moving the files...coz one file we can move by using below command.

rename geetika.seq /root/perl/temp/geetika.seq

but while moving all the seq files its is not allowing to run the below command and throwing the error. File "/root/perl/temp/*.seq" not found.

rename *.seq /root/perl/temp/*.seq

also I didn't find any way to take one file at a time and rename it in sftp. Please help if any one way is possible (all files at a time or one by one...)

Replies are listed 'Best First'.
Re: not able to move files using rename command in sftp
by roboticus (Chancellor) on Aug 20, 2014 at 12:34 UTC

    Ankur_kuls:

    The problem is that you can't just do a rename with wildcards in the names like that. You'll have to move the files one by one. Just get a list of all the matching files, and then in a loop get them and remove them using the same list:

    my @files = ... get list of files to process ...; for my $file (@files) { ... process a file ... ... delete a file ... }

    Aside from the fact that wildcards don't work like you expect them to, there's a race condition in your logic that would occasionally cause you a big problem if it did work that way. Suppose that the remote computer just drops 5 new files into the output directory just as your mget *.seq command ends. Your very next statement rm *seq would delete the files you just processed *and* the 5 new files you haven't yet processed, losing some information. You really have to be careful of things like that. Additionally, since you're using two different wildcards (*seq and *.seq), then you also have the possibility of missing operations when working on files. Be sure to be consistent!

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: not able to move files using rename command in sftp
by Corion (Patriarch) on Aug 20, 2014 at 12:34 UTC

    Where does Perl enter your question?

    Maybe you want to use one of the SFTP modules?