Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

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" = */upload.sh ]] && continue
    ncftpput -R -v -u myUsername -p myPassword myAddress /remoteDir "$i"
done
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 "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...

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.