Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use wildcards in remote directory with sftp

4,575 views
Skip to first unread message

carolus

unread,
Jul 11, 2014, 6:50:22 PM7/11/14
to
This does what I want, opening the remote directory:

$ sftp dell03:junk
Connected to dell03.
Changing to: /Users/cdr/junk

With wildcards it doesn't work; the remote directory is not recognized
as a directory:

$ sftp dell03:"ju*"
Connected to dell03.
Fetching /Users/cdr/junk/ to junk
Cannot download non-regular file: /Users/cdr/junk/

I've tried various quoting methods, and tried terminating the directory
name with /, but nothing worked.

So I connect without wildcard. Then I can use a wildcard here:
sftp> ls ju*
junk/backhd_asus10 junk/deb7xfce20140704 junk/to_bigdrive

but not here:
sftp> cd ju*
Couldn't canonicalize: No such file or directory

The only workaround I've found to avoid typing a long pathname in full
is to save it in an environment variable. Is there a better way?

Kees Nuyt

unread,
Jul 12, 2014, 8:35:35 AM7/12/14
to
On Fri, 11 Jul 2014 17:50:22 -0500, carolus
<sendme...@bellsouth.net> wrote:

>This does what I want, opening the remote directory:
>
>$ sftp dell03:junk
>Connected to dell03.
>Changing to: /Users/cdr/junk
>
>With wildcards it doesn't work; the remote directory is not recognized
>as a directory:
>
>$ sftp dell03:"ju*"
>Connected to dell03.
>Fetching /Users/cdr/junk/ to junk
>Cannot download non-regular file: /Users/cdr/junk/
>
>I've tried various quoting methods, and tried terminating the directory
>name with /, but nothing worked.
>
>So I connect without wildcard. Then I can use a wildcard here:
>sftp> ls ju*
>junk/backhd_asus10 junk/deb7xfce20140704 junk/to_bigdrive
>
>but not here:
>sftp> cd ju*
>Couldn't canonicalize: No such file or directory

That makes sense, because you can not change to more than one
directory at a time.

>The only workaround I've found to avoid typing a long pathname in full
>is to save it in an environment variable. Is there a better way?

Script it in shell with scp, rather than use sftp.
As a bonus, scp can do a bit more with wildcards.
--
Kees Nuyt

carolus

unread,
Jul 12, 2014, 11:38:55 AM7/12/14
to
On 7/12/2014 7:35 AM, Kees Nuyt wrote:

>> So I connect without wildcard. Then I can use a wildcard here:
>> sftp> ls ju*
>> junk/backhd_asus10 junk/deb7xfce20140704 junk/to_bigdrive
>>
>> but not here:
>> sftp> cd ju*
>> Couldn't canonicalize: No such file or directory
>
> That makes sense, because you can not change to more than one
> directory at a time.

This doesn't work either:

sftp> cd ju*/
Couldn't canonicalize: No such file or directory

>
> Script it in shell with scp, rather than use sftp.
> As a bonus, scp can do a bit more with wildcards.

Then the choice is between 1) more scripts than I can keep track of or
2) re-editing a single script, with the same problem of typing long
pathnames, and more steps than an interactive method. I don't see an
advantage over using environment variables as shortcut names for
frequently used directories on the remote machine.

Message has been deleted

carolus

unread,
Jul 12, 2014, 4:48:50 PM7/12/14
to
On 7/12/2014 12:49 PM, Michael Vilain wrote:

> If you can get a listing of the remote files, then you can dump that
> list to a file or variable and iterate through it with a "for" loop.
> This assumes you can scp or sftp without entering a password.
>

For file names, sftp seems to handle wildcards OK, just not directory
names. The problem is navigating the directory tree. If I try to find
my way around the remote directory in sftp by alternating ls and cd, as
I would on the local system (or remotely using ssh), I can't cd without
having to type every letter with no mistakes. So I hunt first for the
target directory with ssh where I can use wildcards, then save the path
to TARGET, and then "sftp $TARGET". To switch targets, I find it easier
to exit and restart sftp than to navigate the remote directory tree
without the help of wildcards.

I just thought there might be an smarter way - some way of quoting, for
example.

0 new messages