I'm trying to move files from one location to another on the remote server using sftp below:

for i in a b c d
sftp $REMUSR <<EOM>>$OUT 2>&1
rename $SOURDIR/sample_${i}_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz $REMDIR

But I get the message:

Couldn't rename file "/source/sample_a_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz" to "/destin/": No such file or directory

Though this file exists under the /source directory which I've verified:

$ ls -l sample_a_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz
-rw-r--r--  1 prd admin 112 May 23 09:16 sample_a_20140330.gz

FTP's rename command doesn't compute regular expressions. It interprets the regex as filename that it not present on the system. You are using sftp. Another approach is to use ssh:

for i in a b c d
  ssh $user@$host "mv $SOURDIR/sample_a_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz $REMDIR"
  • but @chaos, in one of my earlier snippets:for i in a b c d do sftp $REMUSR <<EOM>>$OUT 2>&1 put $LOCAL_DIR/sample_a_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz $REMOTE_DIR quit EOM worked and it placed the sample_a_20140330.gz file on the remote server. Does this mean put computes reg exp but rename does not? Am slightly confused here – user68112 May 23 '14 at 6:33
  • Yes, rename just accepts explicit filenames. A regex or a wildcard could match multiple files. put accepts globs. See the manpage of sftp: put: local-path may contain glob(3) characters and may match multiple files. – chaos May 23 '14 at 6:52
  • If I'm sure of having the files in the required format in the $SOURDIR on the server, is there a way to just move all of them to $REMDIR by giving something like rename $SOURDIR/* $REMDIR? Again, am not sure if rename recognises * here, because it didn't work when I tried:-( – user68112 May 23 '14 at 7:37
  • No, rename does not recognise regexes or globs or wildcarts (*, ?), just simple filenames. – chaos May 23 '14 at 8:16
  • Am trying something like this: for i in a b c d do fname=ls sample_${i}_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz sftp $REMUSR <<EOM>>$OUT 2>&1 rename $SOURDIR/$fname $REMDIR quit EOM : : done ......but no luck – user68112 May 23 '14 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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