Mr-DC
Mr-DC

Reputation: 849

FTP Shell Script mkdir issue

I am using the Bash FTP command to ftp files, however i have a problem where i try to create a directory that is more than 2 folders deep. It works if i use two folders deep but if i go to three folders deep then it fails. For example:

mkdir foo/bar - this works
mkdir foo/bar/baz - this fails

I have also tried this:

mkdir -p foo/bar/baz - which didn't work, it ended up creating a '-p' directory

The shell script i am trying to run is actually quite simple but as you can see the directory structure is 3 folders deep and it fails to create the required folders:

#!/bin/bash
DIRECTORY="foo/bar/baz"
FILE="test.pdf"         
HOST="testserver"           
USER="test"         
PASS="test"         

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
mkdir $DIRECTORY
cd $DIRECTORY
binary
put $FILE
quit
END_SCRIPT

Upvotes: 1

Views: 13515

Answers (2)

mcsrainbow
mcsrainbow

Reputation: 370

Try lftp instead:

[dong@idc1-server1 ~]$ lftp sftp://idc1-server2
lftp idc1-server2:~> ls
drwxr-xr-x    3 dong     dong         4096 Jun 16 09:11 .
drwxr-xr-x   18 root     root         4096 Apr  1 22:25 ..
-rw-------    1 dong     dong          116 Jun 16 09:28 .bash_history
-rw-r--r--    1 dong     dong           18 Oct 16  2013 .bash_logout
-rw-r--r--    1 dong     dong          176 Oct 16  2013 .bash_profile
-rw-r--r--    1 dong     dong          124 Oct 16  2013 .bashrc
drwx------    2 dong     dong         4096 Jul 24  2014 .ssh

lftp idc1-server2:~> mkdir a/b/c/d
mkdir: Access failed: No such file (a/b/c/d)

lftp idc1-server2:~> mkdir -p a/b/c/d
mkdir ok, `a/b/c/d' created

Upvotes: 1

pizza
pizza

Reputation: 7620

mkdir under ftp is implemented by the ftp server, not by calling /bin/mkdir no such options as -p, what you should do is

mkdir foo
cd foo
mkdir bar
cd bar
mkdir baz
cd baz

If you still want your original construct, you can also do it like this:

#!/bin/bash
foo() {
    local r
    local a
    r="$@"
    while [[ "$r" != "$a" ]] ; do
        a=${r%%/*}
        echo "mkdir $a"
        echo "cd $a"
        r=${r#*/}
    done
}
DIRECTORY="foo/bar/baz"
FILE="test.pdf"         
HOST="testserver"           
USER="test"         
PASS="test"         

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
$(foo "$DIRECTORY")
binary
put $FILE
quit
END_SCRIPT

Upvotes: 12

Related Questions