Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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, 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`; do
  ncftpput -R -v -u myUsername -p myPassword myAddress /remoteDir /localDir/$i

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

  1. /localDir/My
  2. /localDir/Directory
share|improve this question
up vote 2 down vote accepted

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" = */ ]] && continue
    ncftpput -R -v -u myUsername -p myPassword myAddress /remoteDir "$i"
share|improve this answer
I didn't think of that. That's a simple yet great way to do it. – SimonBS Dec 29 '11 at 13:07

You could use find to upload all other elements separately:

find /localDir -mindepth 1 -maxdepth 1 ! -name "" -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...

share|improve this answer
Not that FTP works at all with file names containing newlines... – grawity Dec 29 '11 at 13:01
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 Dec 29 '11 at 13:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .