递归列出SFTP上的所有文件

9
我想编写bash脚本,递归列出SFTP上的所有文件(包括完整路径),然后在本地与路径进行交互(因此只需要使用SFTP获取路径)。不幸的是,“ls -R”在那里无法工作。
如果有任何基本的POC方法来实现这个功能,将不胜感激。
Available commands:

bye                                Quit sftp

cd path                            Change remote directory to 'path'

chgrp grp path                     Change group of file 'path' to 'grp'

chmod mode path                    Change permissions of file 'path' to 'mode'

chown own path                     Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                               filesystem containing 'path'
exit                               Quit sftp
get [-Ppr] remote [local]          Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-Ppr] local [remote]          Upload file
pwd                                Display remote working directory
quit                               Quit sftp
rename oldpath newpath             Rename remote file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

1
你尝试过使用“find”命令吗? - 123
抱歉,我忘了提到它也不可用了... - Radek Feber
一个建议:在 software recommendations stackexchange 上询问一个具有此功能或至少可以从bash中进行脚本化的SFTP客户端的建议。 - smheidrich
1
我发布了一个仅使用 echocd 的解决方案,但显然服务器甚至没有 echo 命令,你能验证一下吗?如果是真的,那么就无法实现你的目标。 - ShellFish
2
@RadekFeber 我刚意识到你正在使用SFTP而不是SSH。如果要做任何有意义的事情,你应该使用SSH而不是SFTP。SFTP仅用于文件传输,并且仅提供有限的命令,无法在SFTP中运行脚本。 - 123
显示剩余5条评论
2个回答

6
这个递归脚本可以完成任务:
#!/bin/bash 
#

URL=user@XXX.XXX.XXX.XXX
TMPFILE=/tmp/ls.sftp

echo 'ls -1l' > $TMPFILE

function handle_dir {
  echo "====== $1 ========="
  local dir=$1
  sftp -b $TMPFILE "$URL:$dir" | tail -n +2 | while read info; do
    echo "$info"
    if egrep -q '^d' <<< $info; then
       info=$(echo $info)
       subdir=$(cut -d ' ' -f9- <<< $info)
       handle_dir "$dir/$subdir"
    fi
  done
}

handle_dir "."

填写URL以使用sftp服务器数据。


2
犹豫地+1——也许需要明确指出,这将为每个目录打开一个sftp连接。 - tripleee

5
我扫描整个互联网找到了一个很棒的工具sshfs。通过SSHFS挂载远程目录树。SSHFS是一种远程文件系统,使用SFTP协议访问远程文件。 一旦你挂载了文件系统,你就可以使用所有通常的命令而不必担心文件实际上是远程的。 sshfs对我帮助很大,也许可以为您提供帮助。
mkdir localdir
sshfs user@host:/dir localdir
cd localdir
find . -name '*'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接