t-rex has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys , I am transferring a file on remote host using openssh package using function scp_put(), the place where i intend to transfer this file has to be under certain directory which also should be created by us based on a uid/date. but it throws me error that transfer failed coz directory is not present on remote host. so how do i create this directory remotely.

for example : $sourcedir = "/tmp/xyz/test.tar"; $remotedir = "/tmp/some-dir/myfolder_uid_date/"; ssh->scp_put ( $sourcedir, $remotedir); or die "scp failed";

the output should be i should be able to transfer test.tar in the remote dir path, but i need to create this remote dir path on the fly or from host itself. how can i achieve this ? right now i get error no such file or directory /tmp/some-dir/myfolder_uid_date/

Replies are listed 'Best First'.
Re: creating a directory remotely implicitly
by Corion (Patriarch) on Jun 28, 2016 at 13:22 UTC

    I guess you're using the OpenSSH package.

    If so, it allows you to run remote commands using the ->system method. So, to create a remote directory before copying, you could run:

    $ssh->system(qq{mkdir "$remote_dir"});

      i get the error cannot create directory still aftre using the qq command, is it that we can't create a directory within directory from one single command ? for example : /tmp/some-dir/some-dir2/xyz_uid_date/ now i have to create some-dir/some-dir2/xyz_uid_date/ in one command , how can that be done ?

        how can that be done ?

        According to the mkdir(1) manual page, using the option -p creates the parent directories of the target. Thus,

        $ssh->system(qq{mkdir -p "$remote_dir"});

        will do the trick.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: creating a directory remotely implicitly
by salva (Canon) on Jun 28, 2016 at 18:36 UTC
    Another option, besides running mkdir -p $targetdir, is to use SFTP:
    my $sftp = $ssh->sftp; $sftp->put($sourcefile, $remotefile);
Re: creating a directory remotely implicitly
by perlfan (Parson) on Jun 28, 2016 at 14:12 UTC
    I am not familiar with this package, but I know that with scp, using the recursive (-r) option you may place a hierarchy of directories and files onto the remote machine. Similarly, you can use rsync. With that in mind, you could mirror the structure you want locally, then do a recursive scp or an rsync (using a Perl module hopefully) to achieve the effect you desire.