Exclude file when upload directories

1

I have written a simple shell script which uses ncftpput to recursively upload all directories in a local directory to a remote directory. My shell script, upload.sh is placed in this local directory and I would like to exclude it from the upload. Is there any way to do this?

ncftpput -R -v -u myUsername -p myPassword myAddress /remoteDir /localDir/*

I have tried doing it like this:

for i in `ls /localDir | grep -v upload.sh`; do
  ncftpput -R -v -u myUsername -p myPassword myAddress /remoteDir /localDir/$i
done

But directories with spaces will be considered two directories. E.g. "My Directory" will be considered:

  1. /localDir/My
  2. /localDir/Directory

SimonBS

Posted 2011-12-29T12:54:25.557

Reputation: 301

Answers

2

Which is why you should never parse the output of ls. Especially if you can do the same thing with a simple wildcard.

#!/usr/bin/env bash

for i in /localDir/*; do
    [[ "$i" = */upload.sh ]] && continue
    ncftpput -R -v -u myUsername -p myPassword myAddress /remoteDir "$i"
done

user1686

Posted 2011-12-29T12:54:25.557

Reputation: 283 655

I didn't think of that. That's a simple yet great way to do it. – SimonBS – 2011-12-29T13:07:48.637

6

You could use find to upload all other elements separately:

find /localDir -mindepth 1 -maxdepth 1 ! -name "upload.sh" -exec ncftpput -R -v -u myUsername -p myPassword myAddress /remoteDir {} \;

Skip the -exec and everything after it to just print the matching files and folders, for testing..


You could also look into redefining IFS in bash, or the equivalent in your shell, but that still will fail with file names containing e.g. newlines.

Of course, the simplest solution would be to not mix the uploader script with the data to be uploaded...

Daniel Beck

Posted 2011-12-29T12:54:25.557

Reputation: 98 421

Not that FTP works at all with file names containing newlines... – user1686 – 2011-12-29T13:01:48.400

Your example using find is perfect. Of course, I could have the uploader script outside the directory but as I have more directories with separate upload scripts, I would like to have it in the directory. Thank you very much for your help. – SimonBS – 2011-12-29T13:06:07.657