"Couldn't stat remote file: No such file or directory": Using FTP in shell script from *nix to Win

18,614
sftp>       cd  C:\Users\Tech1\testserverbackup
Couldn't stat remote file: No such file or directory

This is due to FTP servers not having the concept of drive letters for mounts, as Windows systems do.

You can use pwd to while connected to FTP to determine the directory you're currently in:

sftp> pwd
/C/Users/Tech1

...which shows that in your environment, the FTP administrator mapped a Windows C: drive to a regular folder called C within the FTP server (and mapped all the other mounted drives to folders).

Share:
18,614
Jacqlyn
Author by

Jacqlyn

Hello! I started Evil r Us because I was tired of the constant harassment that everyone faces on social media platforms from a small group. I decided to treat trolling like the spam problem it is and started imposing punishments on my social media platform for harassment and bullying. Making consequences for trolling greatly reduced trolling on the platform (surprise, right?). If you would like to donate, see our code, or contribute or contact us in any way about our products, please head to our website.

Updated on June 05, 2022

Comments

  • Jacqlyn
    Jacqlyn almost 2 years

    I'm using a Bash script to backup my webfiles via FTP. As the title says, I have an Ubuntu webserver and am backing up to a Windows machine. I have an ssh program and FileZilla Server on the Windows machine, and can SSH and SFTP into it. The core of the script looks like this:

    SRCDIR="C:\\Users\\Tech1\\testserverbackup"
    DATAIN="/var/www/html/"
    FILENAME="-r *"
    
    sshpass -e sftp -oBatchMode=no -b - ${USER}@${LANHOST} << EOF
    cd  ${SRCDIR}
    lcd ${DATAIN}
    mkdir $(date -I)
    cd $(date -I)
    put ${FILENAME}
    bye
    echo made it
    EOF
    

    The others vars are a bit sensitive, so I don't want to post them, but the credentials have been working for me so far.

    The error I'm getting looks like this:

    sftp>       cd  C:\Users\Tech1\testserverbackup
    Couldn't stat remote file: No such file or directory
    

    I've ssh'd into the folder and sftp'd, so I'm not really sure what the issue is. AFAIK, cd is the native windows command, not just the FTP one.

    Any ideas what's going wrong? Thank you.

    • admdrew
      admdrew almost 10 years
      FTP hosts do not have the concept of mounted drives, as Windows systems do. As the error says, there is no file/directory with C: in the path while connecting via FTP.
    • admdrew
      admdrew almost 10 years
      It means that when you're connected via FTP (ie, on the sftp> prompt), there isn't a C: drive to connect to, so trying to cd to that path simply won't work.
    • admdrew
      admdrew almost 10 years
      cd isn't Windows-specific, that's not the problem. Using C: as part of the file/folder path is Windows-specific, and has no meaning when on an FTP host.
    • admdrew
      admdrew almost 10 years
      You can still use absolute paths, but when connecting to an FTP host, it likely uses the Linux/Unix convention, so it likely starts with / (like the path in your $DATAIN variable).
    • Jacqlyn
      Jacqlyn almost 10 years
      Replacing C:\ with / returns the same error.
    • admdrew
      admdrew almost 10 years
      Yes, the entire path is probably incorrect. What's your output when you type just pwd while logged into FTP?
    • Jacqlyn
      Jacqlyn almost 10 years
      Ah, good point. It's /C/Users/Tech1, so that's what I'll use. Thank you for your help.
    • admdrew
      admdrew almost 10 years
      Ahhh ok perfect, that makes sense. Glad to help!
    • Jacqlyn
      Jacqlyn almost 10 years
      @admdrew Interesting, so that means that / is above C, and going there, I can view all mounted drives.
    • admdrew
      admdrew almost 10 years
      Yes, / is the top-level/root folder. In your environment, the FTP administrator mapped a Windows C: drive to a regular folder called C within the FTP server (and mapped all the other mounted drives to folders).
    • glenn jackman
      glenn jackman almost 10 years
      @jfa, you should answer your own question and accept it, so that others who have the same question don't have to pore through the comments.